PDA

View Full Version : PHP cookie if not set...



thesleuth
September 6th, 2006, 08:27 AM
I have this code which sets a cookie to define the width of a table cell...

if(!isset($_COOKIE['barwidth'])) {
setcookie("barwidth","160");
}(Code 1)

When I clear my cookie and went to that page which has Code 1, the page did not show the table cell with 160, instead is showed a blank (no value). However, after I refresh, the cell showed up as 160. I believed that after the page was loaded, the cookie was set, how do I use the value right after the page is loaded?

thesleuth
September 6th, 2006, 10:21 AM
I also have a file which set cookies and it is in the folder, how do I write the code so that the cookie is set for the whole site and expires after 30 days. (I'm testing this page on my localhost in my site folder)

raz
September 6th, 2006, 05:58 PM
As to why youre cookie wont work for the site if its not set, basically you're right. It checks for the cookie, sets it, but does not read the cookie it just set, unless you set the page to refresh itself or the user refreshes it.


if(!isset($_COOKIE['barwidth'])) {
setcookie("barwidth","160");
$barwidth = 160;
// of that dont work try (if ur HTTP_VARS is off)
// $_COOKIE['barwidth'] = 160;
}

Then to get the 30 day cookie for your whole site just use:



setcookie("name", "value", time()+60*60*24*30, "/", "http://yourdomain.com");


If that doesnt work tell me the error... But I'm pretty sure that should work.

thesleuth
September 6th, 2006, 09:33 PM
Thanks, I've tried
$_COOKIE['barwidth'] = 160; and it works :D

raz
September 9th, 2006, 03:38 PM
Thanks, I've tried
$_COOKIE['barwidth'] = 160; and it works :D

NP - Glad I could help.