Help with cookies

Hi

I was wondering if anyone can help me.

I have a form that filters results.

I save the form data in cookies.

When the user navigates away from the page and comes back I want the search results to stay.

Rather than using

[php]if (!empty($_POST)) {
.
.
.
}[/php]

I tried an if statement to say if a cookie is set then to filter the results but it didn’t work.

Any ideas?

What didn’t work / how didn’t it work? And please add the code you tried.

Also, you could just add the variables to session, then you don’t need to store additional cookies on the users machine.

My form starts off with radio buttons which then reveals the rest of the form.

The radio button value goes into $var1. If cookie is set execute the code to display the results (is my thinking behind it!)

So my code goes like this

[php]setcookie( “var1”, $var1, time() + 36000 );
setcookie( “var2”, $var2, time() + 36000 );

if(!isset($_COOKIE[$var1])) {
//database queries here to display the search results
}[/php]

Would you recommend using session variables instead if cookies? If so how do I execute the form code again when the user returns back to the results page.

Thanks

This is what I do for my websites that I develop:

[php]// Start the session:
$seconds = 60;
$minutes = 60;
$hours = 24;
$days = 14;

session_set_cookie_params($seconds * $minutes * $hours * $days, “”);
session_start();[/php]

I guess I could get more fancy with this, but I’m usually too busy doing something else. :slight_smile:

I’m not so sure sessions is what the OP needs in this case.
He specifically states ‘when users navigate away and come back’
If the user keeps the browser open and returns the session will still remain, however if they close the window and return the session will no longer exist therefore a cookie is the best way to go in this case.

Upon returning read the cookie and do what you need to do with the info.

Just my two-pence.
Red :wink:

[member=26967]Redscouse[/member] going slightly off topic now, but I guess it’s ok. If the session.cookie_lifetime option in php.ini is set to 0 then the session cookie will die on browser close. You should be ok if you set it, either in php.ini or in runtime:

[php]<?php
session_start();
session_set_cookie_params(36002430); // session lives for a month[/php]

Yes thats exactly what I mean.

Do I resubmit the form the user comes back to the page?

Any helps advice is appreciated. Please help!

Thanks

Sponsor our Newsletter | Privacy Policy | Terms of Service