0

Is there a specific cookie Drupal uses to determine if a user is logged in or anonymous?

I need to check the cookie, and create a rewrite condition based on whether or not the user is logged in.

In the end, I would like something like this:

RewriteCond %{HTTP_COOKIE} !drupaluser=1 [NC]
RewriteRule ^(.*)$ /$1?no-cookies-for-you=%1 [R,QSA,L]

Thanks.

7
  • Would you mind sharing what that rewrite condition should be doing? Aug 8, 2016 at 18:16
  • @Pierre.Vriens - edited the question Aug 8, 2016 at 18:19
  • ok, that's a partial answer/clarification for my prior comment, merci! From your question I already understand that you want something to happen (or maybe not happen) if a user is yes or no logged in. But can you elaborate a bit please to explain (in English or Drupaleze) what should happen (or not), depending on yes or no logged in? Aug 8, 2016 at 18:32
  • If user is logged in i would like to set a cookie, if not i would like to redirect to a specific page Aug 8, 2016 at 18:50
  • OK, that's clear. But what about an authenticated user who has disabled cookies (what should happen in that case then)? Would it be an option for you to consider a custom field added to the user profile (updated automatically with whatever info you want to store in your cookie ... which would also work if cookies are disabled)? Aug 8, 2016 at 19:10

1 Answer 1

2

Drupal does store the session value in the cookie variable after the user is logged in. However, the name of this cookie is not fixed. It is basically a string that comes from the value returned by PHP function session_name; and starts with SESS. It would be hard to find and I would not recommend using regex, as I personally don't like it much.

If you really want to implement a solution with the cookie, I would suggest the following:

  1. Implement hook_user_login() in a custom module:

    function MODULE_user_login() {
      // set a new cookie here, with static name and value.
    }
    
  2. Implement hook_user_logout() and delete the cookie by setting an expiry date of past.

You can now use this cookie in your .htaccess!

1
  • I agree with your recommendation about what NOT to do (as in your first parg). And I bet you can guess how I might answer this question (after I know the answer to my 2nd comment below the question) ... Though your answer here, via a custom module, also makes a lot of sense of course (for those who like writing, maintaining and (later on) upgrading custom modules). Aug 8, 2016 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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