cookie problem!

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

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

cookie problem!

Post by shiznatix »

im trying to assign a cookie with a diffrent value for every user so the cookie has your user ID. heres my code

Code: Select all

$sessID = session_id();
if (!isset($_COOKIEї'cartID'])){
setcookie('cartID', $sessID, time()+60*60*24*30, '/', '', 0);
}

$cartID = $_COOKIEї'cartID'];
but it dosn't work properly, when i close my browser and open it back up to destroy the session the cookie is no longer set. with this at the top of the script i would always have a cookie am i correct? but i tried adding in this right after those lines of code

Code: Select all

if (!isset($_COOKIEї'cartID'])){
die('You Must Have Cookies Enabled');
}
but when i destroyed the session it would give me the die error, but i didnt delete the cookies? plus wouldnt the cookie already be set anyway if it did not exist by the lines directly before this? im confused
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

your code works for me real fine! But One thing, the code you posted doesn't have a session_start in the start though. Have you also left it out in your original script ? Maybe that is causing it not to work.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

do you switch parts of domains throughout the site? For instance, if you start at http://www.foo.com and transfer to foo.com the cookie will not exist on foo.com .. this is due to how you are setting it. A domain value of '.foo.com' will make the cookie available to all of the domain. Note the leading dot.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

ok now its working (kinda?) i dunno its real strange and works sometimes and other times it does not. maybe its my browser? i use firefox. dunno
Post Reply