realjumper Posted November 2, 2006 Share Posted November 2, 2006 Hi,I set a cookie on the company website (I'm at home now so I can't test), like this...[code]setcookie("login", $username, time()+3600, "/");[/code]If I don't specify the path "/" then the cookie doesn't get set in the root of the web server which is no good. So, everything is fine.Now, I need to change the cookie so that it expires on browser close so I just removed the time()+3600, which resulted in no cookie! So I tried entering time()+0, and even just leaving it empty completely like this '', but still nothing. I have just read that if I try this....[code]setcookie("login", $username, 0, "/");[/code]...as I say, I'm at home now so I can't test it, but does that look correct? I have to specify the path, but I want the cookie to expire on browser close.Thanks :) Link to comment https://forums.phpfreaks.com/topic/25895-cookie-question/ Share on other sites More sharing options...
.josh Posted November 2, 2006 Share Posted November 2, 2006 if you want a cookie to expire on browser close then why not just use sessions? Link to comment https://forums.phpfreaks.com/topic/25895-cookie-question/#findComment-118263 Share on other sites More sharing options...
realjumper Posted November 2, 2006 Author Share Posted November 2, 2006 Well, because I'd rather use cookies!! Link to comment https://forums.phpfreaks.com/topic/25895-cookie-question/#findComment-118269 Share on other sites More sharing options...
.josh Posted November 2, 2006 Share Posted November 2, 2006 okay then set the cookie to sometime in the past, make sure it's a full day or two, to accomodate for time zone differences. Link to comment https://forums.phpfreaks.com/topic/25895-cookie-question/#findComment-118272 Share on other sites More sharing options...
realjumper Posted November 2, 2006 Author Share Posted November 2, 2006 Okay, great, thank you :) Link to comment https://forums.phpfreaks.com/topic/25895-cookie-question/#findComment-118275 Share on other sites More sharing options...
CBStrauss Posted November 2, 2006 Share Posted November 2, 2006 Just set the value to -3600 to expire it :D Link to comment https://forums.phpfreaks.com/topic/25895-cookie-question/#findComment-118447 Share on other sites More sharing options...
realjumper Posted November 2, 2006 Author Share Posted November 2, 2006 Okay, I'm at work now and this is what happens......If I set a negative value to the time, the cookie won't set.[code]setcookie ("username", "$username", time()-53600, "/"); //doesn't set at all[/code]But if I do what the manual says, even though I doubted it(!!), it works great and expires on browser close, which is what I want. Can someone explain to me what's happening here please?[code]setcookie ("username", "$username", 0, "/"); // works perfectly, expires on browser close which is great[/code] Link to comment https://forums.phpfreaks.com/topic/25895-cookie-question/#findComment-118639 Share on other sites More sharing options...
.josh Posted November 3, 2006 Share Posted November 3, 2006 you are setting a cookie called username, and it's holding the user's name, your 3rd argument is 0 which is the same as not providing an expiration time (which tells the cookie to expire when the browser closes), and then the / is the path on the server where the cookie can be used. What you did there is for all intents and purposes the same as this:setcookie("username", $username);I can't be sure since I don't know your setup or anything, but you are probably running into problems setting the cookie without the path argument due to something along the lines of your script being somewhere for instance in another directory on the same level as your public_html or possibly even anywhere besides public_html/ like public_html/blah/moreblah/script.php I'm not an expert on the finer points in cookies, but I know I ran into a similar problem a while back, and I remember it had something to do with that. anyways, if you do not supply an expiration argument, then the cookie will expire when the browser is closed. The reason why you need a 0 is if you need to add the 4th argument (the path the cookie can used on). the 0 is a null value "placeholder" for the 3rd argument, so that you may supply a 4th (or 5th, etc..) argument Link to comment https://forums.phpfreaks.com/topic/25895-cookie-question/#findComment-118971 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.