Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set cookie wih JS, read with PHP problem

I'm trying to set a cookie with javascript and read it in an other page with php. I am able to write the cookie by doing

document.cookie = cookieName+"="+cookieValue; 

and i partially works. - The cookie is written, and I am able to read it with $_COOKIE[cookieName] but ONLY in the same web page.

Which is not quite usefull really. I need to read it in another page. I usually develop in asp.net and c#, so I'm preety new to php. Am I doing something wrong?

Thank you for your time!

EDIT1: both pages are in the same domain.. eg. site.com/index.php -> site.com/index2.php

EDIT2: the cookie is set in one page through:

function SetCookie(cookieName,cookieValue,nDays) {  var today = new Date();  var expire = new Date();  if (nDays==null || nDays==0) nDays=1;  expire.setTime(today.getTime() + 3600000*24*nDays);  document.cookie = cookieName+"="+escape(cookieValue)                  + ";expires="+expire.toGMTString(); } 

and in another page it can not be accessed, but in that same page it can...

EDIT3: i tried setting the domain and added path=<?php echo $_SERVER['HTTP_HOST']; ?> to the javascript code... still nothing..

EDIT4: so far I have..

document.cookie = cookieName+"="+escape(cookieValue)+"; expires="+expire.toGMTString()+"; path=/"+"; domain=.<?php echo $_SERVER['HTTP_HOST']; ?>"; 

and still i can read the cookie ONLY from the same page..

EDIT5: oh.. my .. god... it was a typo all along... just needed to remove the" path=/"+"; dom..." i am so ashamed of myself right about now... in the meantime i also reset my cookies, so Jared now i unfortuneatly can't accept your post as anwser... i brought shame upon my name!!!....

like image 502
Andrej Avatar asked Feb 18 '11 18:02

Andrej


People also ask

Can PHP read a js cookie?

cookie php cannot assess the cookie, php can only assess cookie set by document. cookie . print_r($_COOKIE); you will see your javascript created cookie inside, among other created by php.

How will you set cookies using PHP?

Modify a Cookie Value $cookie_name = "user"; $cookie_value = "Alex Porter"; setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");

Can cookies be set by JavaScript?

JavaScript can also manipulate cookies using the cookie property of the Document object. JavaScript can read, create, modify, and delete the cookies that apply to the current web page.

How do you set and retrieve cookies in PHP?

Setting Cookie In PHP: To set a cookie in PHP, the setcookie() function is used. The setcookie() function needs to be called prior to any output generated by the script otherwise the cookie will not be set. Syntax: setcookie(name, value, expire, path, domain, security);


1 Answers

Read on setting Javascript cookies and in particular path and domain access here:

http://www.quirksmode.org/js/cookies.html

I think what is happening is one of two things:

  1. You aren't accessing the cookie from the same domain/subdomain, and/or
  2. the other page is not part of the path the cookie has specified.

So your cookie is not giving the relevant information to the browser for it to be accessible across subdomains and/or the directory path.

document.cookie = 'ppkcookie1=testcookie; expires=Thu, 2 Aug 2001 20:47:11 UTC; path=/; ;domain=.example.com' 

Note, .example.com is just an example domain (you need yours in there), and you do not need a wildcard other than the initial . for it go across all subdomains. And you need to generate an expires= date. From QuirksMode:

function createCookie(name,value,days) {     if (days) {         var date = new Date();         date.setTime(date.getTime()+(days*24*60*60*1000));         var expires = "; expires="+date.toGMTString();     } else {         var expires = "";     }     document.cookie = name+"="+value+expires+"; path=/; domain=.example.com"; } 

I added the domain= bit to QuirksMode's function.

EDIT (The example below originally referenced pages on my personal website.)

Andrej, this works perfectly fine for me:

http://example.com/test.php

function createCookie(name,value,days) {     if (days) {         var date = new Date();         date.setTime(date.getTime()+(days*24*60*60*1000));         var expires = "; expires="+date.toGMTString();     }     else var expires = "";     document.cookie = name+"="+value+expires+"; path=/; domain=.example.com"; }  createCookie('cookieee','stuff','22'); 

http://example.com/test/test.php

<pre> <?php   print_r($_COOKIE);  ?> 

And the printout of $_COOKIE will show the cookie. Note I when I inspect the cookie the .example.com is set correctly as the domain.

like image 196
Jared Farrish Avatar answered Sep 19 '22 08:09

Jared Farrish