views:

4228

answers:

5

Hi,

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

+1  A: 

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.

FGM
+5  A: 

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%5Fcookies.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'];
}
?>
John Fiala
-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.
Jeremy French
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 :)
Saurabh
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.
John Fiala
A: 

That was awesome... good work.. it was really helpful...

andy
A: 

Thanks John - I've had problems setting cookies for anonymous users in Drupal before (Drupal seemed to be doing something to block it - very odd) but am going to give this a try!

Cheers

Jason

jpamental
+1  A: 

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!

R.Aubin

related questions