I want to add a unique identifier in a cookie when the user (anonymous or logged) visits the website. Where should I call this code. I tried calling the code in hook_init but that doesn't work on cached pages. Any help would be greatly appreciated. Thanks.


I think hook_boot() would be the best place for your code:

This hook is run at the beginning of the page request. It is typically used to set up global parameters which are needed later in the request.

Only use this hook if your code must run even for cached page views.This hook is called before modules or most include files are loaded into memory. It happens while Drupal is still in bootstrap mode.

(emphasis mine)

| improve this answer | |
  • I've tried hook_boot() too. It seems to be I need to change the value bootstrap=1 in the {system} table according to this comment to make it work. api.drupal.org/api/drupal/developer!hooks!core.php/function/… Would there be any other way around this? – zarsiwala May 31 '12 at 23:14
  • 1
    I wouldn't have thought so, but you could make sure it happens when your module is enabled using a hook_enable() function: function MYMODULE_enable() { db_query("UPDATE system SET boostrap = 1 WHERE name = 'MYMODULE'"); }, and maybe do the opposite in hook_disable(). That way at least you're not manually editing the db – Clive May 31 '12 at 23:18
  • Thanks Clive, hook_boot() along with hook_enable() , hook _disable() did the trick. Thanks for your help! – zarsiwala Jun 5 '12 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.