SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Cookie Reading

  1. #1
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cookie Reading

    i know i can do
    PHP Code:
    setcookie("name""value"time() + 15768000"""www.yourdomain.com"); 
    to set a cookie to last one year, but how would i read from that cookie and use it's info in a web page?
    And would that info be usable over many different pages?
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  2. #2
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you're asking how to read the cookies? and yes, they're available on any pages that have access to that cookie (if that made sense ). in your example, that cookie would be available on any page at www.yourdomain.com. if you set the path to '/folder/', then it's only available from /folder/ and its subdirectories.

    to read the cookie in your example you can use $name or the preferred way, with $HTTP_COOKIE_VARS['name']. it's all explained on the setcookie() page of the PHP manual.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  3. #3
    Digital Warrior Renegade's Avatar
    Join Date
    Nov 2000
    Location
    Portland, OR
    Posts
    480
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    a variable would be created in PHP in the name of the cookie.

    $name would exist in your example, its value would be

    $name=value

    You could also make it an array, like this:

    PHP Code:
    setcookie("name[field1]""value1"time() + 15768000"""www.yourdomain.com");

    setcookie("name[field2]""value2"time() + 15768000"""www.yourdomain.com"); 
    so, we have:
    $name[field1] = value1
    and
    $name[field2] = value2

    clear as mud?
    --There's my 1.5 cents, now where is my change!?!?

  4. #4
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So if i put $name or $HTTP_COOKIE_VARS[name] in a script that hadn't set the cookie it'll read from $name as if it'd been made in the script?
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  5. #5
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, exactly. If the data is sensitive in some way (IE: a userid or username, etc)...I highly recommend adding a line like this at the top of your script:

    PHP Code:
    $var $HTTP_COOKIE_VARS["var"]; 
    If you don't, someone could basically specify a value for "var" in the querystring and potentially access someone else's data.

  6. #6
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what does that do though?
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  7. #7
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like I said, it prevents people from specifying the variable value through the querystring...it ensures that the value of $var (in that example) is indeed a value from a cookie, a whole cookie, and nothing but a cookie.

  8. #8
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by TWTCommish
    a cookie, a whole cookie, and nothing but a cookie.
    ... so help me Keebler Elves?

    ok i see now.
    thx.
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*


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
  •