Page 1 of 2
cookie help
Posted: Tue Jun 19, 2007 9:45 pm
by tecktalkcm0391
I want to set a session variable that has an array in it to a cookie, and then everytime a page is loaded update the cookie with new information. And if the session is killed for some reason, I want the cookie to be read and reset the session value. I have this so far....
Code: Select all
if(isset($_SESSION['info'])){
setcookie("info", serialize($_SESSION['info']), time()+21600, '/'); // Last 6 hours
} elseif(isset($_COOKIE['info'])){
$_SESSION['info'] = unserialize($_COOKIE['info']);
}
I am looking at the cookies in FireFox, and I am getting different cookie values for the info for different directories, when they all should be the same. Should I be doing after the isset($_S... line a check and if cookie $_COOKIE... = $_SESSION... and if not set cookie?
Posted: Tue Jun 19, 2007 9:59 pm
by feyd
Is the domain remaining constant as well in these cookies?
Posted: Tue Jun 19, 2007 10:15 pm
by tecktalkcm0391
yes. they are all for the same domain.
Posted: Tue Jun 19, 2007 10:22 pm
by feyd
No subdomain changes?
Posted: Tue Jun 19, 2007 10:27 pm
by aaronhall
I'm curious as to why you would store data in a cookie that's effectively dependent on another cookie (the session), when you have full control over both.
Posted: Tue Jun 19, 2007 10:44 pm
by tecktalkcm0391
No subdomain changes. just site.com
I want to store a cookie that lasts, because the information is for a shopping cart, and I want the cart to remain active, but if the user is logged-in it still logs them out after the session expires (browser closes)
Posted: Tue Jun 19, 2007 10:56 pm
by feyd
Why not save their cart to a set of (database) tables?
Posted: Tue Jun 19, 2007 11:18 pm
by tecktalkcm0391
feyd wrote:Why not save their cart to a set of (database) tables?
I was thinking about that, but how would it just reappear when then come back if its in 6 hours.
Posted: Tue Jun 19, 2007 11:23 pm
by feyd
They would have to log in again, unless somehow they were still logged in.
Posted: Tue Jun 19, 2007 11:35 pm
by tecktalkcm0391
Ok, but the visitor doesn't have to have an account. Example: officedepot.com you go to there website. you can add stuff to your shopping cart, login, and out, exit the browser, and open it again and your shopping cart stays. thats what I am trying to accomplish.
Posted: Wed Jun 20, 2007 12:38 am
by tecktalkcm0391
Ok, I narrowed it down to something with serialize and unserialize not working right. Any ideas? Testing some codes, I'll see if I can figure out why, but help would be greatly appreciated.
Posted: Wed Jun 20, 2007 12:43 am
by feyd
What's in it?
Posted: Wed Jun 20, 2007 5:19 am
by superdezign
tecktalkcm0391 wrote:feyd wrote:Why not save their cart to a set of (database) tables?
I was thinking about that, but how would it just reappear when then come back if its in 6 hours.
You could use the cookie only to store the id of their cart in the database (and maybe another value for verification.. a "password" if you will), and then access their cart from that. No serialization would have to be done at all in this case.
Posted: Wed Jun 20, 2007 8:50 am
by tecktalkcm0391
feyd wrote:What's in it?
$_SESSION['info'] = '1,1,2,3';
Posted: Wed Jun 20, 2007 9:24 am
by feyd
It's a string? Don't store that in a cookie. The size of cookies is severely limited. Use the database. Set their session cookie to have an expiration then use that as the key (in some form) to the database records.