/    Sign up×
Community /Pin to ProfileBookmark

[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:

[code=php]setcookie (“please”, “work”);[/code]

It works. I get the cookie named please, with a value of work.

When I try to set a cookie with this code:

[code=php]$working = ‘work’;
setcookie (“please”, “$working”);[/code]

It works. I get the cookie named please, with a value of work.

When I try to set a cookie with this code:

[code=php]$email = $_POST[’email’];
setcookie (“please”, “$email”);[/code]

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:

[code=php]$email = “$_POST[’email’]”;
setcookie (“please”, “$email”);[/code]

Notice the quotes around $_POST[’email’]. When I did that, I got this error returned:

[QUOTE]

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

[/QUOTE]

So, for giggles, I changed the single/double quotes around to this:

[code=php]$email = ‘$_POST[“email”]’;
setcookie (“please”, “$email”);[/code]

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:

[QUOTE]

%24_POST%5B%22email%22%5D

[/QUOTE]

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?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 19.2008 — [code=php]
$email = '$_POST["email"]';
[/code]

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 [i]not[/i] be quoted. What makes this confusing is that when not within a double-quoted string, they need to be quoted. :rolleyes: 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:
[code=php]
$email = $_POST['email'];
if(empty($email))
{
die("email is empty: '$email'");
}
setrawcookie("email", rawurlencode($email));
[/code]

If using PHP4, try this instead of setrawcookie:
[code=php]
header('Set-Cookie: ' . rawurlencode('email') . '=' . rawurlencode($email));
[/code]
Copy linkTweet thisAlerts:
@trevzillaauthorApr 19.2008 — 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:
[code=php]$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));[/code]


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!
Copy linkTweet thisAlerts:
@NogDogApr 19.2008 — 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:
[code=php]
header('Set-Cookie: ' . rawurlencode('name') . '=' . rawurlencode($name), false);
[/code]
Copy linkTweet thisAlerts:
@trevzillaauthorApr 19.2008 — Haha! That works! Thank you SO much for all your help! You get an A++. ?
×

Success!

Help @trevzilla spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...