Jump to content


Photo

PHP Problem with cookies


  • Please log in to reply
2 replies to this topic

#1 sammuts

sammuts

    Newbie

  • New Members
  • Pip
  • 1 posts

Posted 25 June 2013 - 08:04 AM

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?



#2 dalecosp

dalecosp

    Advanced Member

  • Members
  • PipPipPip
  • 307 posts
  • LocationJoplin, MO

Posted 25 June 2013 - 08:28 AM

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.


"God doesn't play dice" --- Albert Einstein
"Perl is hardly a paragon of beautiful syntax." --- Weedpacket

#3 dalecosp

dalecosp

    Advanced Member

  • Members
  • PipPipPip
  • 307 posts
  • LocationJoplin, MO

Posted 25 June 2013 - 08:34 AM

  • 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, 25 June 2013 - 08:34 AM.

"God doesn't play dice" --- Albert Einstein
"Perl is hardly a paragon of beautiful syntax." --- Weedpacket




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com