javascript - Set cookie wih JS, read with PHP problem -


i'm trying set cookie javascript , read in other page php. able write cookie doing

document.cookie = cookiename+"="+cookievalue; 

and partially works. - cookie written, , able read $_cookie[cookiename] in same web page.

which not quite usefull really. need read in page. develop in asp.net , c#, i'm preety new php. doing wrong?

thank time!

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

edit2: cookie set in 1 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 page can not accessed, in same page can...

edit3: tried setting domain , added path=<?php echo $_server['http_host']; ?> javascript code... still nothing..

edit4: far have..

document.cookie = cookiename+"="+escape(cookievalue)+"; expires="+expire.togmtstring()+"; path=/"+"; domain=.<?php echo $_server['http_host']; ?>"; 

and still can read cookie same page..

edit5: oh.. .. god... typo along... needed remove the" path=/"+"; dom..." ashamed of myself right now... in meantime reset cookies, jared unfortuneatly can't accept post anwser... brought shame upon name!!!....

read on setting javascript cookies , in particular path , domain access here:

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

i think happening 1 of 2 things:

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

so cookie not giving relevant information browser accessible across subdomains and/or directory path.

document.cookie = 'ppkcookie1=testcookie; expires=thu, 2 aug 2001 20:47:11 utc; path=/; ;domain=.example.com' 

note, .example.com example domain (you need yours in there), , not need wildcard other initial . go across subdomains. , need generate expires= date. 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 domain= bit quirksmode's function.

edit (the example below referenced pages on personal website.)

andrej, works fine 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 printout of $_cookie show cookie. note when inspect cookie .example.com set correctly domain.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -