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.

Link to comment
Share on other sites

  • 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 by dalecosp
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.