10

After several days and more hours I only found issues and patches about what to do once a cookie is set, but not one is mentioning how you get a cookie set in the first place.

Our intention would be to set a (session) cookie of the landing page (for ALL user roles) and retrieve that cookie on certain pages, through views, to use in the URL of an outbound link. This would be very simple in standard programming (even I understand) but I am starting to have considerable doubt if this is actually possible in Drupal.

4

Drupal already sets a session cookie when a user logs in: see the function drupal_session_regenerate().

Can you use this session Id for your purposes? If not, then you just need to create a new value in the cookie the same way that you will do in PHP, $_COOKIE['my_value'];. Place this code in the hook_init() of a custom module, adding a if() to filter the landing page.

Later, you retrieve it and use it in your view; you can use some of the hooks of Views depending on your needs. You can also use the module Views custom field, and embed your PHP code there.

  • 1
    If all he needs it's a session value, then he can use $_SESSION[] too, as a session is always initialized from Drupal. – kiamlaluno May 22 '11 at 23:29
2

You can set a cookie with: setcookie('cookiename',$valueOfCookie);

You can retrieve the cookie value with: $_COOKIE['cookiename']

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.