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']; ?> Quote 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 Quote 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 Quote 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. Quote 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. Quote 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 Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.