Solved

PHP setcookie() - how do I get '@' to be represented in the content?

Posted on 2011-02-16
7
359 Views
Last Modified: 2012-06-21
Hi,

I am trying to set a cookie with an email address.  If I use this code:

<?php
setcookie('ThisIsATest','test@test.com');
?>

Open in new window


Then I get a cookie with the following:
Name: ThisIsATest
Content: test%40test.com

Is there a way to make that content value 'test@test.com'?

Thanks,
Rick
0
Comment
Question by:rickmatt
7 Comments
 
LVL 8

Expert Comment

by:dmeeren
ID: 34913692
@ is a special sign, so you need to put a "\" in front of it:

setcookie('ThisIsATest','test\@test.com');

Open in new window

0
 
LVL 4

Expert Comment

by:florjan
ID: 34913696
maybe define
<?php
$string="test@test.com" 
setcookie('ThisIsATest','$sting');
?>

Open in new window

0
 
LVL 8

Expert Comment

by:rationalboss
ID: 34913735
Hi,

When using PHP to set cookies, just use the normal:
setcookie('test','hello@test.com');

Open in new window


When you want to access the cookie via PHP, just use:
echo $_COOKIE['test'];

Open in new window


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>";
?>

Open in new window


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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Expert Comment

by:rationalboss
ID: 34913744
I forgot to show the example about unescape:

<?php
setcookie('test','hello@world.com');
echo "Cookie set: " . $_COOKIE['test'] . '<br />JS normal: ';
echo "<script type=\"text/javascript\">document.write(document.cookie)</script>";
echo "<br />JS unescaped: <script type=\"text/javascript\">document.write(unescape(document.cookie))</script>";
?>
0
 

Author Comment

by:rickmatt
ID: 34915352
Thanks for the answers, but perhaps I need to clarify.

<?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 />";

?>

Open in new window


Yields the following output:

test = test@test.com
test2 = test2\@test.com
test3 = test3@test.com

But when I view the cookies in Firefox using Tools>>Options>>Privacy>>View 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
0
 
LVL 8

Accepted Solution

by:
rationalboss earned 250 total points
ID: 34916863
There is no need to escape @ signs. In fact, they shouldn't be.
What you did was right, and the values are automatically escaped by the browser (special symbols are converted to their corresponding % equivalent). It is stored that way, @ = %40. But this would not interfere with how you access them via PHP. Just remember to unescape() the cookie if you are accessing it via Javascript.

You can be sure that when you set: setcookie('test','something@something.here');
$_COOKIE['test'] is something@something.here
0
 

Author Closing Comment

by:rickmatt
ID: 34917202
Thanks for the clarification.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now