sammuts Posted June 25, 2013 Share Posted June 25, 2013 I have the following code <?php setcookie("name", "Ivan Sammut"); echo $_COOKIE["name"]; ?> The first time the php gives an error but if I refresh the page then the value is displayed. Can anyone help? Quote Link to comment https://forums.phpfreaks.com/topic/279550-php-problem-with-cookies/ Share on other sites More sharing options...
dalecosp Posted June 25, 2013 Share Posted June 25, 2013 I have the following code <?php setcookie("name", "Ivan Sammut"); echo $_COOKIE["name"]; ?> The first time the php gives an error but if I refresh the page then the value is displayed. Can anyone help? That's normal. The cookie is placed in the browser's cache, but since the page has already been loaded, the cookie wasn't present at page load. Since you already know the "name", put it in another variable ... it will be usable there. Quote Link to comment https://forums.phpfreaks.com/topic/279550-php-problem-with-cookies/#findComment-1437790 Share on other sites More sharing options...
dalecosp Posted June 25, 2013 Share Posted June 25, 2013 (edited) Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. Expire time is set via the expire parameter. A nice way to debug the existence of cookies is by simply calling print_r($_COOKIE);. Edited June 25, 2013 by dalecosp Quote Link to comment https://forums.phpfreaks.com/topic/279550-php-problem-with-cookies/#findComment-1437791 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.