Using strings in cookies?

Discussion in 'PHP' started by misterdh, Aug 5, 2010.

  1. #1
    Hi

    I am having troubles using a string in a cookie..

    This doesnt work:

    setcookie("background", "$image_name", $expire, "/", ".gedoo.com");
    PHP:
    Neither does this:

    setcookie("background", "" . $image_name . "", $expire, "/", ".gedoo.com");
    PHP:
    If I write something before $image_name like:

    setcookie("background", "hello" . $image_name . "", $expire, "/", ".gedoo.com");
    PHP:
    It will save "hello" in the cookie.. So in other words, $image_name doesnt look like it is defined.. But the funny thing is that this works on the same page:

    echo "File Uploaded Successfully!<br><img src='images/$image_name'>";
    PHP:
    Anyone have any idea on what I am doing wrong?
     
    misterdh, Aug 5, 2010 IP
  2. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #2
    You may want to check to see whether or not the variable $image_name is actually populated with data at the time you're attempting to set the cookie.
     
    Alex Roxon, Aug 5, 2010 IP
  3. misterdh

    misterdh Peon

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes it is .. Actually it is echoed right before like:

     	echo "File Uploaded Successfully!<br><img src='images/$image_name'>";
     }
    
    
    $expire=time()+60*60*24*3000;
    setcookie("background", "$image_name", $expire, "/", ".gedoo.com");
    PHP:
     
    misterdh, Aug 5, 2010 IP
  4. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #4
    There's a closing bracket right after it. Could that perhaps be a conditional statement that isn't being met? Is the present HTTP host gedoo.com? Perhaps you could try something simpler:

    
    $expire=time()+60*60*24*3000;
    setcookie("background", $image_name, $expire);
    print_r( $_COOKIE );
    
    PHP:
    edit: you'll have to refresh once for the $_COOKIE variable to be populated, since the option to set-cookie is part of the returned HTTP header.
     
    Alex Roxon, Aug 5, 2010 IP
  5. misterdh

    misterdh Peon

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Nope nothing :( I refresh every time ..
     
    misterdh, Aug 5, 2010 IP
  6. misterdh

    misterdh Peon

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I figured it out !

    I was sending output before I was calling setcookie ... :p

    Im a newbie BTW
     
    misterdh, Aug 5, 2010 IP
  7. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #7
    if it's echoed before, that would send the header meaning the cookie can't be set iirc. Set the cookie before you send any text

    for testing what cookies have been set personally use
    
    foreach ($_COOKIE as $v => $a) {
     echo "$v - $a<br/>";
    }
    
    PHP:
     
    themullet, Aug 5, 2010 IP
  8. techbongo

    techbongo Active Member

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #8
    For tracking sessions always prefer to go for Sessions, not Cookies. It's efficient, secure and fast. However, if you want to store user data permanently, you can't go with sessions.
     
    techbongo, Aug 5, 2010 IP