5

I want to store the zip code of a user, if they use it for a proximity search in a cookie. However, I don't know where or how to start to implement this.

How do I store anonymous user information on a cookie?

10

Since Drupal 7, there are two functions to store data in a cookie: user_cookie_save(), and user_cookie_delete(). Despite the function names, the functions work also for anonymous users, since they don't get information from the $user object or any information that is related to the user ID.

comment_form_submit() uses user_cookie_save() in the following code lines:

if (user_is_anonymous()) {
  user_cookie_save(array_intersect_key($form_state['values'], array_flip(array('name', 'mail', 'homepage'))));
}

If you are using a version of Drupal that doesn't define that functions, you can implement similar functions in a module you use.

function mymodule_cookie_save(array $values) {
  foreach ($values as $field => $value) {
    // Set cookie for 365 days.
    setrawcookie('Drupal.visitor.' . $field, rawurlencode($value), REQUEST_TIME + 31536000, '/');
  }
}

function mymodule_cookie_delete($cookie_name) {
  setrawcookie('Drupal.visitor.' . $cookie_name, '', REQUEST_TIME - 3600, '/');
}

Keep in mind that setrawcookie() is only available in PHP 5. If you are developing a module that could be used with PHP 4, then you should use setcookie(), and the code of the functions should be accordingly changed: the parameter needs to be declared as $values (not array $values), and the call to rawurlencode() needs to be removed as the cookies are already URL encoded from setcookie().

| improve this answer | |
  • 2
    If you are developing code for Drupal 6, creating such functions would make your code compatible with Drupal 7; when porting the code to Drupal 7 you would just need to change the calls to mymodule_cookie_save() with user_cookie_save(). You could also use user_cookie_save() and user_cookie_delete() for your functions, if you first check those functions are not already defined, which would happen if another module defines them for Drupal 6. – kiamlaluno May 31 '11 at 14:28
3

Setting cookies in Drupal is not different from how you would do it with any other PHP site:

setcookie is the function that PHP provides for this. There is also the $_COOKIE super global variable to use.

Code example:

$cookieval = 'my cookie value';
if ($_COOKIE['my_cookie_key'] != $cookie_val) {
 setcookie('my_cookie_key', $cookie_val);
}
| improve this answer | |
  • The only example of this I can find is in the flag module which records anonymous users flags in client cookies. Perhaps it's the object oriented PHP, but the code is a little intimidating for me. I'll look at it again since from your suggesting its the write direction. – Adam S May 31 '11 at 11:06
  • @Adam, I've added a code example of how you could set a cookie value (and how to get it) it is really not that hard. – googletorp May 31 '11 at 11:11
  • I'll use $_COOKIE['location'] value for the default value of an exposed Views form for a proximity search. On submit I'll just check to see if the $_COOKIE['location'] value does not equal the $form_state value in which case I'll use the setcookie() function as you described above. – Adam S May 31 '11 at 11:23
0

If you have Drupal installed in subfolder so that your site root URL has the form www.mydomain.com/mysite, you should probably use base_path() instead of '/' when setting the cookie.

Functions in kiamlaluno's answer would then be:

function mymodule_cookie_save(array $values) {
  foreach ($values as $field => $value) {
  // Set cookie for 365 days.
  setrawcookie('Drupal.visitor.' . $field, rawurlencode($value), REQUEST_TIME + 31536000, base_path());
  }
}

function mymodule_cookie_delete($cookie_name) {
  setrawcookie('Drupal.visitor.' . $cookie_name, '', REQUEST_TIME - 3600, base_path());
}
| improve this answer | |

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.