www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] Revisting the Setcookie() Function

  1. #1
    Join Date
    Mar 2008
    Posts
    65

    resolved [RESOLVED] Revisting the Setcookie() Function

    Firstly, I am starting a new thread because I would like to get some new eyes on this problem. I have another thread going, but it is getting long, drawn out and hard to read. With that said, here is my problem:

    When I try to set a cookie with this code:
    PHP Code:
    setcookie ("please""work"); 
    It works. I get the cookie named please, with a value of work.

    When I try to set a cookie with this code:
    PHP Code:
    $working 'work';
    setcookie ("please""$working"); 
    It works. I get the cookie named please, with a value of work.

    When I try to set a cookie with this code:
    PHP Code:
    $email $_POST['email'];
    setcookie ("please""$email"); 
    It doesn't work. No cookie is even written.

    when I echo $_POST['email'] I get the email address I entered in the previous form.

    The ONLY difference I see is that I have a quote around 'work' and no quote around $_POST['email'].

    So, I did this:
    PHP Code:
    $email "$_POST['email']";
    setcookie ("please""$email"); 
    Notice the quotes around $_POST['email']. When I did that, I got this error returned:
    Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/content/t/r/e/trevzilla/html/getfile.php on line 4
    So, for giggles, I changed the single/double quotes around to this:
    PHP Code:
    $email '$_POST["email"]';
    setcookie ("please""$email"); 
    And I finally got a cookie to write. But guess what the value is! It sure isn't an email address! The Value of the cookie came out to be this:
    %24_POST%5B%22email%22%5D
    So, with that long drawn out explanation, does anyone know what is going on here? Why isn't a cookie writing with the email address in the value?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,933
    PHP Code:
    $email '$_POST["email"]'
    Variables are not interpolated within single quotes, so you are setting $email to the literal string value '$_POST["email"]', not the value of the post variable. (The '%24_POST%5B%22email%22%5D' you see is just the urlencoded value, which is how the strings have certain characters "escaped".)

    You should not have to quote the variables at all when all you want is the value of the variable. The unexpected T_ENCAPSED_AND_WHITESPACE error is because of a quirk in PHP: associative array indexes for an array variable being used within a double-quoted string must not be quoted. What makes this confusing is that when not within a double-quoted string, they need to be quoted. But again, there's no reason to be quoting it in this example.

    Assuming you're positive the email value is populated, all I can think is that there's something about it that messes up the urlencoding. If using PHP 5, see what happens with this:
    PHP Code:
    $email $_POST['email'];
    if(empty(
    $email))
    {
       die(
    "email is empty: '$email'");
    }
    setrawcookie("email"rawurlencode($email)); 
    If using PHP4, try this instead of setrawcookie:
    PHP Code:
    header('Set-Cookie: ' rawurlencode('email') . '=' rawurlencode($email)); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Mar 2008
    Posts
    65
    Very nice! One step closer! So I tried that with one cookie named 'email' and sure enough it worked. So, after taking one or two victory laps around my room, I sat back down to do some copying and pasting to get that code to work with the second cookie I need to set. 'name' to be specific.

    So, here is my code:
    PHP Code:
    $email $_POST['email'];
    $name $_POST['firstname']." ".$_POST['lastname'];

    if(empty(
    $email))
    {
       die(
    "email is empty: '$email'");
    }
    header('Set-Cookie: ' rawurlencode('email') . '=' rawurlencode($email));

    if(empty(
    $name))
    {
       die(
    "name is empty: '$name'");
    }
    header('Set-Cookie: ' rawurlencode('name') . '=' rawurlencode($name)); 
    As you can see, I'm using PHP 4 still. Well, when I ran that code, I only got one cookie created, and that was of the 'name' variety. It seemed to skip over 'email' entirely. Hopefully this is an easy fix, but I sure don't see what to do. Thanks for any help!

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,933
    Forgot about that: you need to tell header() not to replace headers of the same type (which is the default behavior), so add a false for the optional 2nd parameter:
    PHP Code:
    header('Set-Cookie: ' rawurlencode('name') . '=' rawurlencode($name), false); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Mar 2008
    Posts
    65
    Haha! That works! Thank you SO much for all your help! You get an A++.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles