xbl1 Posted April 25, 2008 Share Posted April 25, 2008 Hi:I am testing how to retrieve a cookie value, but i could not retrieve a cookie value properly. could anyone help, thank.my browser is Firefox 2.0.0.14, and i went to the following path to pick a cookiename, and try to print out the content of the cookie, sometime it works, sometimes it does not work.tool-> options-> showcookies and <?phpecho $_COOKIE["homepage"];?> Link to comment Share on other sites More sharing options...
Lulzim Posted April 25, 2008 Share Posted April 25, 2008 One thing to note about cookies is that you cannot retrieve a cookie immediately after you send it.for example: <?phpsetcookie("test","test");echo $_COOKIE['test']?> if you open this page for the first time, echo $_COOKIE["test"] will not output anything because first the cookie is sent to the browser, next time when you open this page again the browser sends the cookie back to server and then you will see it.That's why as you say sometimes (for the first time) you can't see it sometimes you can. Link to comment Share on other sites More sharing options...
xbl1 Posted April 25, 2008 Author Share Posted April 25, 2008 One thing to note about cookies is that you cannot retrieve a cookie immediately after you send it.for example:<?phpsetcookie("test","test");echo $_COOKIE['test']?> if you open this page for the first time, echo $_COOKIE["test"] will not output anything because first the cookie is sent to the browser, next time when you open this page again the browser sends the cookie back to server and then you will see it.That's why as you say sometimes (for the first time) you can't see it sometimes you can. Thanks, you are right. it is better to put setting a cookie value and writting a cookie value in different script. Link to comment Share on other sites More sharing options...
xbl1 Posted April 26, 2008 Author Share Posted April 26, 2008 I am sorry that the same problem still not solve.my code as following:login_process3.php if(isset($_SESSION['login_remember'])){ $email= $_SESSION['login_email']; $id=getId($email); setcookie("qq", "", time()-3600); setcookie("qq", $id, time()+60*60*24*360, "/"); header ("location: index.php"); } else{ $email= $_SESSION['login_email']; $id=getId($email); setcookie("qq", "", time()-3600); setcookie("qq", $id, time()+60*60*24*1, "/"); header ("location: index.php"); } ///////////////////index.php <?phpif (isset($_COOKIE["qq"])){ $id=$_COOKIE["qq"]; $DisplayName=getDisplayName($id); echo "<li id=\"welcome\"> Welcome to " . $DisplayName . " !</li>";}else{ echo (" <li><a href=\"Login.php\" >Login</a></li>");}?> Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.