Solved

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

Posted on 2011-02-16
7
397 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

710 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