Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 988
Sbhambry Avatar asked Aug 22 '09 21:08

Sbhambry


1 Answers

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'];
}
?>
like image 173
John Fiala Avatar answered Sep 20 '22 15:09

John Fiala