What is the latest and best way to set a cookie upon login? What I mean is: if a user succesfully logins, then a session cookie user-is-logged-in = 1 is set. When the user logs out, this is removed.

I see this https://www.drupal.org/project/logincookie but its very old and doesn't look supported.. I see this but https://www.drupal.org/node/2160739 but it starts with a requirement with Varnish which I don't have..

I am using Drupal version 7.43.

As you know session cookies set by Drupal don't do the trick, as anonymous users are also set a cookie and from a browser's perspective, they are not differentiable.

Thanks a lot for your help!

  • Added hopefully more details above. this has nothing to do with the Cookie law, i'm just wondering what is the state of the art for setting a cookie based on a login action, if I need to code or add a module or use the regular functionalities. Thanks – JoeSlav Apr 1 '16 at 10:02

You could just write the code yourself in a custom module. What you want is pretty simple.

 * Implements hook_user_login().
function HOOK_user_login(&$edit, $account) {
  setcookie('user-is-logged-in', '1', 0, '/');

 * Implements hook_user_logout().
function HOOK_user_logout($account) {
  // Set cookie in the past and then remove it.
  setcookie('user-is-logged-in', '', REQUEST_TIME - 3600, '/');
  • Thanks! I followed drupal.org/developing/modules/7 and it looks like I basically create a .module file with your code; a .info with some sample description and it's all set -- do i miss anything? Thanks a lot! – JoeSlav Apr 1 '16 at 11:54
  • mmmm.. it doesn't work :) trying to find the issue.. I load the module fine, the code is executed (if I insert syntax errors I break the site), but i don't see my cookie :( – JoeSlav Apr 1 '16 at 12:55
  • 1
    @JoeSlav You need to replace HOOK with the module name, that's how hooks work – googletorp Apr 1 '16 at 14:22
  • thanks a lot, that was a stupid RTFM mistake on my part :) for completeness, the unset call didn't work -- I had to use a seetcookie with expire date in the past to get the cookie unset. cheers and thanks again – JoeSlav Apr 1 '16 at 15:02
  • @JoeSlav Great, updated the answer with the complete solution – googletorp Apr 1 '16 at 15:05

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.