15

I have a theoretical question..

I know that you can get/read a PHP cookie with javascript by using: document.cookie

Is there a similar way to do this in PHP?

Can PHP get/read a cookie that is created i JavaScript? If yes, then how can you do that?

Spoofy
  • 621
  • 4
  • 9
  • 27
  • 1
    You can also set a cookie in PHP with the setcookie function. What really happens is that the response will carry a cookie header; the browser will then store the cookie. Each request from now on will be relayed in the request header, where PHP and other server-side scripts access what's been stored before. – Schien Mar 14 '14 at 00:57

6 Answers6

21

You can use $_COOKIE, the superglobal. Just reference it like you would any array, where $_COOKIE['key_name'] is the cookie you want to access.

See the PHP API documentation.

gnarf
  • 105,192
  • 25
  • 127
  • 161
jacobroufa
  • 371
  • 2
  • 8
  • 1
    So i just need to use $_COOKIE['cookiename'] i the php code ad php will get the cookie that have been set i JS? – Spoofy Mar 14 '14 at 00:49
  • [this](https://stackoverflow.com/a/5045117/875020) has a little bit more on using the right domain when setting the cookie in js, for people landing here – x29a Nov 27 '18 at 15:45
1

To see them you can do this:

foreach($_COOKIE as $v){
  echo htmlentities($v, 3, 'UTF-8').'<br />';
}

For a single cookie it's just:

echo htmlentities($_COOKIE['cookieName'], 3, 'UTF-8');

Feel free to change the quote style (3 being ENT_QUOTES) and charset to suit your needs.

Note: The cookie has to have been set on the same domain for you to access it.

totallyNotLizards
  • 8,489
  • 9
  • 51
  • 85
StackSlave
  • 10,613
  • 2
  • 18
  • 35
1

PHPglue's answer is good, but has several typos in it. It also doesn't tell you what the index is which can be very helpful.

foreach($_COOKIE as $key=>$value)
{
  echo "key: ".$key.'<br />';
  echo "value: ".$value.'<br />';
};

My issue was that I was setting my cookie in Javascript with periods in the name. These were being converted to underscores. For example cookie name, facebookLogin.myapp.com was being changed to facebookLogin_myapp_com. Once I ran the code above, I was able to see that the name was different than expected and read the name from PHP correctly.

Justin Domnitz
  • 3,217
  • 27
  • 34
1

Javascript can set cookies in two ways (of i know) window.cookie and document.cookie. if use window.cookie php cannot assess the cookie, php can only assess cookie set by document.cookie.

javascript

document.cookie = 'name=value';

PHP

print_r($_COOKIE);

you will see your javascript created cookie inside, among other created by php.

or

echo $_COOKIE['name'];

Assess php cookie with javascript use same document.cookie to assess document (php) cookies and javascript cookies created with document.cookie.

Javascript only can assess window.cookies created cookies

chokey2nv
  • 11
  • 3
1

HTTP is a stateless protocol. During Client Server communication to maintain state we use some techniques. Cookie is one of them. Generally we use Cookies to identify an user. When first time user send request to the server in response server sends a set of Cookies. Which browser stores in Client machine. Next time when the same user request again browser send the information store in Cookies. Through which server get to identify the user. Create, Remove or Read a PHP Cookie using Setcookie method

vedamalhar
  • 11
  • 2
0

I think this is a good way to set all intimation in one cookie and use an symbol between your information. Then use for example this in "PHP" after set cookie by JavaScript

$mystr = print_r($_COOKIE);
$way=explode(";",$mystr);
Arslan Ali
  • 17,418
  • 8
  • 58
  • 76