SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist Jhorra's Avatar
    Join Date
    Sep 2004
    Location
    Phoenix, Az
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problems handling cookies with javascript and PHP

    On one page I add items to a cookie using javascript, then on a separate page I read them. The problem I'm having is that I can read them, but when I try to change the value of the cookie that was set with javascript.

    I read the cookie $_COOKIE['rentals'], but when I try and clear that cookie using $_COOKIE['rentals'] = ""; it doesn't do anything, because on the page I'm redirecting to I echo out the value of the cookie, and it's still there. Is there an issue with setting a cookie with javascript and reading it with PHP?

  2. #2
    SitePoint Enthusiast traxxas's Avatar
    Join Date
    Jan 2007
    Location
    San Diego, CA
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cookies are sent from the client side to the server. You need to removed them from the client so they aren't sent to the server and set by PHP. If you want PHP to control the state from page to page then sessions is what you should be using.

  3. #3
    SitePoint Evangelist Jhorra's Avatar
    Join Date
    Sep 2004
    Location
    Phoenix, Az
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But you can obviously read and set cookies using PHP, so if I can read it why in this case can I not set the value?

  4. #4
    SitePoint Enthusiast traxxas's Avatar
    Join Date
    Jan 2007
    Location
    San Diego, CA
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because PHP can't delete a cookie from the serverside. According to the docs:

    When deleting a cookie you should assure that the expiration date is in the past, to trigger the removal mechanism in your browser.
    http://us.php.net/manual/en/function.setcookie.php

  5. #5
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You cannot write to cookies using $_COOKIE, you must use setcookie() and overwrite the old one.

  6. #6
    SitePoint Evangelist Jhorra's Avatar
    Join Date
    Sep 2004
    Location
    Phoenix, Az
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, thank you.

  7. #7
    SitePoint Evangelist Jhorra's Avatar
    Join Date
    Sep 2004
    Location
    Phoenix, Az
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, should this work, changing $_COOKIE['rentals'] = ""; to setcookie('rentals', ''); ?

  8. #8
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    706
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    The manual is a terrible thing to waste.
    PHP Code:
    <?php
    // set the expiration date to one hour ago
    setcookie ("TestCookie"""time() - 3600);
    setcookie ("TestCookie"""time() - 3600"/~rasmus/"".example.com"1);
    ?>


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
  •