cookie help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

cookie help

Post 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?
Last edited by tecktalkcm0391 on Tue Jun 19, 2007 10:15 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Is the domain remaining constant as well in these cookies?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

yes. they are all for the same domain.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

No subdomain changes?
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Why not save their cart to a set of (database) tables?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

They would have to log in again, unless somehow they were still logged in.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What's in it?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

feyd wrote:What's in it?
$_SESSION['info'] = '1,1,2,3';
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply