Results 1 to 6 of 6

Thread: Cookie help

  1. #1
    Join Date
    Apr 2010
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Cookie help

    I am new to php and just started doing a bit of stuff with cookies and now got slightly stuck.

    I am fully aware on how to create a cookie, make a cookie invalid.

    im also aware of
    PHP Code:
    print_r($_COOKIE); 
    to print the value of the cookie on the page... but what if i wanted $thecookie value to be the value of the cookie then wanted to print $thecookievalue

    Im not going to echo $thecookievalue i just need to value of the cookie in a string so i can set an if statement to check what the value of the cookie is so i can set the correct page of the user.

    Thanks

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    To get the cookie value, wouldn't you just do $_COOKIE['name'] and then manipulate that?
    Jeremy | jfein.net

  3. The Following User Says Thank You to Nile For This Useful Post:

    Tim Dobson (04-10-2010)

  4. #3
    Join Date
    Apr 2010
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Yea sure i can do that but new to all this stuff and what ever iv tried has failed just need an example to show me how its done lol

  5. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    It sounds like what you want is to retrieve the cookie value from a cookie without actually manipulating the cookie itself.

    $thecookievalue=$_COOKIE['name'];

    that's it.
    To choose the lesser of two evils is still to choose evil. My personal site

  6. The Following User Says Thank You to james438 For This Useful Post:

    Tim Dobson (04-10-2010)

  7. #5
    Join Date
    Apr 2010
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Ah yes i did that and it pulled out an error but i figured it out so i asked because i wasent sure if this was what i was doing wrong but thanks

  8. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    What is the error? Just like any variable, if you try to use a variable that has no value it will give you a warning (not an error, but similar) if the error reporting is high enough. You can fix this by making sure it's set first:
    $var = ''; //set it to blank
    if (isset($_COOKIE['var'])) { $var = $_COOKIE['var']; }

    There are lots of other ways to approach it as well. You can also just lazily suppress the error, but that's not a great idea (since it can be unpredictable), but at least you won't get an error message for visitors:
    echo @$_COOKIE['var'];
    (the @ symbol means 'and don't show an error if there is one')
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •