SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru Dashman's Avatar
    Join Date
    Jan 2006
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cannot display cookie data

    Hi guys,

    I am setting a cookie in a login system. The code I have which sets the cookie is as follows:
    PHP Code:
    $cookieName 'myCookie';
    $value 'testValue';
    $expire 0;
    $secure TRUE;
    $httpOnly TRUE;
    if(
    setcookie($cookieName$value$expire''''$secure$httpOnly)){
          
    // this always returns true, so cookie does get set
          
    return true;
     }else{
          return 
    false;

    I have done various tests to make sure the cookie is getting set, and it is. My FF web dev tool bar also displays the cookie with the correct data.

    Now, the problem I have, is no matter where I try to do so, I cannot print out or retrieve the data from this cookie. Only the PHP session id is ever displayed:
    PHP Code:
    if($_COOKIE['myCookie']){
        print 
    "COOKIE SET<br />";
        
    print_r('myCookie');
    }else{
        print 
    "COOKIE NOT SET";

    Using isset does not work either:
    PHP Code:
    if(isset($_COOKIE['myCookie'])){
        print 
    "COOKIE SET<br />";
        
    print_r('myCookie');
    }else{
        print 
    "COOKIE NOT SET";

    Why could this be? How am I supposed to retrieve the data from the cookie?

  2. #2
    SitePoint Enthusiast stakes's Avatar
    Join Date
    Sep 2008
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because you've set the "secure" parameter to true which indicates that the cookie should only be transmitted over a secure HTTPS connection.

    (I'm guessing your not doing this over https://)
    webcloud.se - Articles on web development

  3. #3
    SitePoint Guru Dashman's Avatar
    Join Date
    Jan 2006
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahhhh!

    Great, thanks, that works if I set secure to false now. I figured that it should be OK as it was still setting the cookie ...

    Thanks very much

  4. #4
    SitePoint Enthusiast stakes's Avatar
    Join Date
    Sep 2008
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also I don't know in what context your script is in, but using "return" outside the scope of a function will terminate your script. Which means in your code example:

    Code:
    if(setcookie($cookieName, $value, $expire, '', '', $secure, $httpOnly)){
        // this always returns true, so cookie does get set
        echo "script terminates here";
        return true;
    
    }    
    echo "this wont run.";
    webcloud.se - Articles on web development

  5. #5
    SitePoint Guru Dashman's Avatar
    Join Date
    Jan 2006
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey ... it is part of a login class I am building, so this function tries to set the cookie, if it does, the method that calls it will do something, and do something else if it fails


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
  •