SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Aug 2004
    Location
    Australia
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Cookie Setting/Retrieving

    Hi Guys,

    I'm setting a cookie using php using:
    setcookie('cookiename', 'cookiedetails');

    And when i use:
    echo $_COOKIE['cookiename'];

    Nothing gets echo'd back untill i refresh the page again. And if i close the window and open up a new browser and go back to the page, the cookie doesn't exist anymore....

    I checked my Windows Cookie directory and doesn't seem to be saving any cookie in there.

    Any idea what im doing wrong or i've got the wrong idea?

    Chino

  2. #2
    My precious!!! astericks's Avatar
    Join Date
    Mar 2002
    Location
    Vancouver, BC
    Posts
    1,971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this:

    setcookie('cookiename', 'cookiedetails', time()+3600);

    this sets a cookie with a 1hour expiry time from the moment it was set.

  3. #3
    SitePoint Zealot
    Join Date
    Aug 2004
    Location
    Australia
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wows look like that expiry time did the trick! So i gather if there is no expiry time set, it kinda expires immediately?

    Cheers

  4. #4
    SitePoint Zealot stdlist's Avatar
    Join Date
    Sep 2004
    Location
    Corvallis, OR, USA
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cheeky Chino
    Nothing gets echo'd back untill i refresh the page again.
    This is correct. The setcookie function places the cookie in the response that goes back to the browser. So the cookie does not get added to the $_COOKIE array until to refresh the page.

    Quote Originally Posted by Cheeky Chino
    Wows look like that expiry time did the trick! So i gather if there is no expiry time set, it kinda expires immediately?
    If there is no value specified, then the cookie will expire when the session ends or basically when the brower closes.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •