cookie reading

Discussion in 'PHP' started by dizyn, Oct 16, 2009.

  1. #1
    Hi

    I wanted to see if cookie is set or not...

    For example i have site example.com and there is directory named member in it

    i set a cookie in that directory i.e. example.com/cookie

    can i read and/or access that cookie value from example.com ?

    if i can please tell me how...

    my domain name:
    example.com
    i set a cookie at:
    example.com/member

    and now i wanted to read it from example.com
     
    dizyn, Oct 16, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You would need to set the path parameter of setcookie (http://php.net/setcookie) to '/' and then you'll be able to access it from your root as well.

    So:

    setcookie('key', 'value', 0, '/');
     
    premiumscripts, Oct 16, 2009 IP
  3. dizyn

    dizyn Active Member

    Messages:
    251
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    thank you but i wanted to know that i am at example.com and cookie is at example.com/members

    is there any way i can access it form example.com

    thank you
     
    dizyn, Oct 16, 2009 IP
  4. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yes... if you write it as I said in /members.. Otherwise, no.
     
    premiumscripts, Oct 16, 2009 IP
  5. dizyn

    dizyn Active Member

    Messages:
    251
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5
    urrently i am at example.com/members

    I set a cookie "test_cookie"

    I can access it here but i wanted to access it when i go back to example.com

    I tired it this way:

    <?PHP
     print_r($_COOKIE);
    ?>
    Code (markup):
    but it's not showing my anything

    but when i go to example.com/members and use same code i get value
     
    dizyn, Oct 16, 2009 IP
  6. mgutt

    mgutt Active Member

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    90
    #6
    use path / and domain .example.com (attention to the point). now you can access the cookie on the complete website including subdomains and folders. but you need to delete your cookies first, as it is not possible in all browsers to set a cookie with the same name with a different path and/or domain.

    php.net:
     
    mgutt, Oct 16, 2009 IP
  7. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Set the cookie like I told you.. Do that code on /members. Then access it via the $_COOKIE array on your root.
     
    premiumscripts, Oct 16, 2009 IP