How to store session/cookie in Drupal 6 on the computer of the visitor

22,168

Solution 1

Of course it can be done, but it is not the normal Drupal way of doing it. What you typically do is store the information either in the session (use sess_write/sess_read) or, if the user is logged-in, in the user account info.

Keeping information in the cookie means it can be tampered with on the client side, and you'll need to make your code extra foolproof, as with any user-originated data.

If you keep it in the session, it will be kept for as long as the session lives, but disappear thereafter. For anonymous users, there is really no better way. OTOH, for logged-in users, storing the info in $user->data means they will be available as long as you don't remove them, because they'll be stored in the users tables.

Also remember that drupal session cookies are regenerated when switching from anonymous to logged-in, but keep the session content, so you can put data in the anonymous session and still have it when the user logs in, and then store them in his account data. Look at includes/session.inc for details about how this works.

Solution 2

If you need to keep track of something and it needs to work with the user not logged in, what you want to do is use a cookie to keep track of it. You can't use the session to store things for anonymous visitors in Drupal, because it's tied to the user object.

There's a description here - http://www.w3schools.com/PHP/php_cookies.asp - but let me go into it.

You start off using setcookie(name, value, expire) - we'll assume that we want to call this value business_search, and we'll use a test value of '80204', which is a zip code - this works just as fine if you use 'Denver, CO'. We don't want it to expire for, oh, six months, so we would want to call:

setcookie('business_search', '80204', time() + 3600 * 24 * 180);

That time there represents approximately six months worth of seconds being added to the time right now.

Getting the cookie afterwards is even easier - just use $_COOKIE['business_search'], and it will return the value. So, we could just use this code:

<?php
function saveSerch($search_term) {
  setcookie('business_search', $search_term, time() + 3600 * 24 * 180);
}

function readSearch() {
  return $_COOKIE['business_search'];
}
?>

Solution 3

If you're having trouble getting your client-side cookies to stick in Drupal, try using the optional parameter for path.

example:

setcookie('myCookieName', 'myCookieData', 0, '/');

Then, on the next page load output your cookies to the screen. You should see your cookie in the output.

print_r($_COOKIE);

-or more specifically-

print $_COOKIE['myCookieName'];

Cheers!

Share:
22,168
Sbhambry
Author by

Sbhambry

Everyday I'm explorin'! Explorin', Explorin'!

Updated on July 09, 2022

Comments

  • Sbhambry
    Sbhambry almost 2 years

    I have created a search for a client website using Finder module in conjunction with Views module. The problem is that my client wants that each time a user selects and searches for the businesses in a certain area, then the same should be stored as session or cookie on the computer of the visitor. This would ensure that a repeat visitor sees the same search which he had done earlier. Please check the site: http://naplesres.designbracket.com/

    Moreover, I also want to know if based on the search earlier can i configure the Views in a manner that on only business of his local area are passed in the rest of the views used on the website i.e. on the Auto, Beauty etc. pages.

    Any help in form of links to documents, pointers as to how to go about it would be deeply appreciated.

    Thanks

  • hasni
    hasni over 14 years
    -1 . This is a very nice description of cookies, but the question was asked with regards to the Drupal framework. This answer doesn’t discuss sessions in Drupal and could cause confusion when trying to integrate with it.
  • Sbhambry
    Sbhambry over 14 years
    I'd say dat a -1 is being a bit too harsh .. I found the answer pretty helpful and this in conjunction with the answer above does help clarify quite a few things. I'd only say Thank You John :)
  • John Fiala
    John Fiala over 14 years
    I was answering in regards to the Drupal framework, and I did discuss sessions in Drupal in the first paragraph. To wit, "You can't use the session to store things for anonymous visitors in Drupal, because it's tied to the user object." So, i explain how to do with with cookies, which will work with anonymous visitors.
  • AgA
    AgA over 12 years
    In FGM's answer above, it looks like we can use session to store data even for anonymous users.
  • AgA
    AgA over 12 years
    I've found this link which states that for even anon users session is created except for that it gets created from second visit onwards: books.google.ca/…
  • Robert Husák
    Robert Husák about 2 years
    Beware that sess_write and sess_read are intented for internal use of Drupal. It supplies them to session_set_save_handler, ensuring that $_SESSION is always written and read from the database ($key is the session ID). Therefore, storing session-related data into $_SESSION should suffice (at least it does in Drupal 6). Using sess_write and sess_read directly might have severe consequences, such as unintended changing of currently logged-in user (speaking from experience :-) ).