?
Solved

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

Posted on 2011-02-16
7
Medium Priority
?
407 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
Independent Software Vendors: 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!

 
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 1000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

719 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