Solved

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

Posted on 2011-02-16
7
342 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

758 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

14 Experts available now in Live!

Get 1:1 Help Now