rickmatt
asked on
PHP setcookie() - how do I get '@' to be represented in the content?
Hi,
I am trying to set a cookie with an email address. If I use this code:
Then I get a cookie with the following:
Is there a way to make that content value 'test@test.com'?
Thanks,
Rick
I am trying to set a cookie with an email address. If I use this code:
<?php
setcookie('ThisIsATest','test@test.com');
?>
Then I get a cookie with the following:
Name: ThisIsATest
Content: test%40test.com
Content: test%40test.com
Is there a way to make that content value 'test@test.com'?
Thanks,
Rick
maybe define
<?php
$string="test@test.com"
setcookie('ThisIsATest','$sting');
?>
Hi,
When using PHP to set cookies, just use the normal:
When you want to access the cookie via PHP, just use:
However, the problem comes when you try to access the cookie using JavaScript.
Make sure you use unescape() before outputting the cookie. I've attached an example below:
Make sure that you reload the page during your first run to see the results - it will be empty during your first run. Cookies are not accessible in the document right after you set them because cookies rely on the browser requests, and in that connection, the browser just received the cookie and it is already finished sending the requests.
When using PHP to set cookies, just use the normal:
setcookie('test','hello@test.com');
When you want to access the cookie via PHP, just use:
echo $_COOKIE['test'];
However, the problem comes when you try to access the cookie using JavaScript.
Make sure you use unescape() before outputting the cookie. I've attached an example below:
<?php
setcookie('test','hello@world.com');
echo "Cookie set: " . $_COOKIE['test'] . '<br />JS: ';
echo "<script type=\"text/javascript\">document.write(document.cookie)</script>";
?>
Make sure that you reload the page during your first run to see the results - it will be empty during your first run. Cookies are not accessible in the document right after you set them because cookies rely on the browser requests, and in that connection, the browser just received the cookie and it is already finished sending the requests.
I forgot to show the example about unescape:
<?php
setcookie('test','hello@wo rld.com');
echo "Cookie set: " . $_COOKIE['test'] . '<br />JS normal: ';
echo "<script type=\"text/javascript\">d ocument.wr ite(docume nt.cookie) </script>" ;
echo "<br />JS unescaped: <script type=\"text/javascript\">d ocument.wr ite(unescape(document.cookie))</script >";
?>
<?php
setcookie('test','hello@wo
echo "Cookie set: " . $_COOKIE['test'] . '<br />JS normal: ';
echo "<script type=\"text/javascript\">d
echo "<br />JS unescaped: <script type=\"text/javascript\">d
?>
ASKER
Thanks for the answers, but perhaps I need to clarify.
Yields the following output:
But when I view the cookies in Firefox using Tools>>Options>>Privacy>>V iew Cookies, then I see those urlencoded values ('@' >> %40, etc).
I see that other cookies are storing these characters un-urlencoded, and I was wondering if I can do that using PHP. It may be that those other cookies are created with other technologies.
Thanks,
Rick
<?php
$name = 'test';
$content = 'test@test.com';
setcookie($name,$content);
setcookie('test2','test2\@test.com');
setcookie('test3','test3@test.com');
echo "test = " . $_COOKIE[$name] . "<br />";
echo "test2 = " . $_COOKIE['test2'] . "<br />";
echo "test3 = " . $_COOKIE['test3'] . "<br />";
?>
Yields the following output:
test = test@test.com
test2 = test2\@test.com
test3 = test3@test.com
test2 = test2\@test.com
test3 = test3@test.com
But when I view the cookies in Firefox using Tools>>Options>>Privacy>>V
I see that other cookies are storing these characters un-urlencoded, and I was wondering if I can do that using PHP. It may be that those other cookies are created with other technologies.
Thanks,
Rick
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the clarification.
Open in new window