Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-02-16
7
Medium Priority
?
419 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
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!

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses
Course of the Month11 days, 19 hours left to enroll

916 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