stuckwithcode Posted July 19, 2010 Share Posted July 19, 2010 why does the code below not work first time, the value 1 is only echoed after a refresh <?php session_start(); setcookie("random",1,time()+600000,"/"); echo $_COOKIE['random']; ?> Link to comment https://forums.phpfreaks.com/topic/208202-these-are-both-server-side-so-why-does-it-not-work/ Share on other sites More sharing options...
Rifts Posted July 19, 2010 Share Posted July 19, 2010 because there is nothing there the first time. you are setting the cookie and then once you refresh its there Link to comment https://forums.phpfreaks.com/topic/208202-these-are-both-server-side-so-why-does-it-not-work/#findComment-1088237 Share on other sites More sharing options...
stuckwithcode Posted July 19, 2010 Author Share Posted July 19, 2010 I still don't get it, if you look at the code below why does the cookie take a refresh but the variable doesn't then. Could you explain in detail please. <?php session_start(); setcookie("random",1,time()+600000,"/"); echo $_COOKIE['random']; $variable = 1; echo $variable; ?> Thanks Link to comment https://forums.phpfreaks.com/topic/208202-these-are-both-server-side-so-why-does-it-not-work/#findComment-1088241 Share on other sites More sharing options...
PFMaBiSmAd Posted July 19, 2010 Share Posted July 19, 2010 The $_COOKIE['random'] variable is set by the browser when the browser requests the page. Link to comment https://forums.phpfreaks.com/topic/208202-these-are-both-server-side-so-why-does-it-not-work/#findComment-1088242 Share on other sites More sharing options...
Pikachu2000 Posted July 19, 2010 Share Posted July 19, 2010 The cookie data is not available until the next page load. Read the first paragraph of this. Link to comment https://forums.phpfreaks.com/topic/208202-these-are-both-server-side-so-why-does-it-not-work/#findComment-1088247 Share on other sites More sharing options...
stuckwithcode Posted July 19, 2010 Author Share Posted July 19, 2010 thanks guys, problem solved Link to comment https://forums.phpfreaks.com/topic/208202-these-are-both-server-side-so-why-does-it-not-work/#findComment-1088250 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.