Jump to content

PHP Problem with cookies


sammuts

Recommended Posts

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.

  • 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);.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.