Storing anonymous user information in a cookie
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 =&gt; $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().