Beauford Posted April 26, 2008 Share Posted April 26, 2008 I have a page that has a form. When the user enters their name and hits submit they go to another page where a cookie is set. The problem is that the cookie is not available to that page. I know the cookie is set, because if I close the browser and reopen and check the cookie all the info is there. This is my code: <? session_start(); if (!$_COOKIE["user"]) { $expiretime = 60 * 60 * 24 * 365 + time(); setcookie("user", $_POST['name'], $expiretime); } echo $_COOKIE["user"]; Rest of Page ?> The above gives me nothing, nor does any other reference to the cookie on the page. If I close the browser and reopen and echo $_COOKIE["user"];, it is there. So it is being set, so why can't I access it as in the above code. Note: I also have session_start(); at the top of the page as there are some session variables I need which don't need to be in the cookie. Thanks Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted April 26, 2008 Share Posted April 26, 2008 The cookies IS set, but you can't do anything with it until the next page load. In the manual it says: Once the cookies have been set, they can be accessed on the next page load Quote Link to comment Share on other sites More sharing options...
DarkWater Posted April 26, 2008 Share Posted April 26, 2008 Cookies are not available on the page that you set them. Quote Link to comment Share on other sites More sharing options...
Beauford Posted April 27, 2008 Author Share Posted April 27, 2008 Kinda screws what I'm trying to do then. I didn't see that part about the reload, but I guess it makes sense. Thanks Quote Link to comment Share on other sites More sharing options...
Fadion Posted April 27, 2008 Share Posted April 27, 2008 Use $_SESSION instead. Quote Link to comment 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.