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
  • 591
  • 4
  • 8
  • 25
  • 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

5 Answers5

20

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
  • 103,494
  • 24
  • 125
  • 159
jacobroufa
  • 361
  • 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,209
  • 9
  • 48
  • 84
StackSlave
  • 10,436
  • 2
  • 17
  • 33
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,077
  • 26
  • 33
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
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
  • 16,978
  • 8
  • 56
  • 70