简体   繁体   中英

php cannot recognize javascript cookie

I know there is no difference between JavaScript cookies and PHP cookies. yet. I'm setting a cookie with JavaScript, and checking it with PHP.

When I check the cookie with JavaScript, it returns as set. But when I check in PHP, it returns as not set. Here's my code:

Javascript

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
  {
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
   if (x==c_name)
    {
    return unescape(y);
    }
   }
  }

function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
 document.cookie=c_name + "=" + c_value;
}

function moomoo()
{
var username=getCookie("username");
if (username!=null && username!="")
  {
  alert("Welcome again " + username);
  }
else 
  {
  username=prompt("Please enter your name:","");
  if (username!=null && username!="")
    {
    setCookie("moomoo",username,365);
    }
  }
}

HTML

<div onclick="moomoo();">click me</div>

PHP

if (isset($_COOKIE["moomoo"])) {
echo ' moomoo worked'; }
else {
echo ' moomoo didnt work';}

When I recall the moo moo() script. It alerts with my name

When I load the PHP script it says "moo moo didn't work".

THE FIX!:

Thank you for the suggestions everyone. Alas, the solution was much simpler and as usual, a stupid error. Originally the function moo moo() read:

function moomoo()
{
var username=getCookie("**username**");
if (username!=null && username!="")
  {
  alert("Welcome again " + username);
  }
else 
  {
  username=prompt("Please enter your name:","");
  if (username!=null && username!="")
    {
    setCookie("**username**",username,365);
    }
  }
}

Therefore the first time I called the function it set a cookie named "username". Then i changed it to how it is now. So since username was already set, it didn't set moomoo. So the php function couldn't find moomoo cause it was never set. Thank you all!

When I recall the moo moo() script. It alerts with my name

It looks like the moomoo variable is not getting set in JavaScript. If your name is alerted, the else block isn't being executed.

Try checking your username cookie in php, and it will display.

Try to set the Domain and Path for the cookie.

function setCookie(c_name,value,exdays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()) + ";domain=.mydomain.com;path=/";
    document.cookie=c_name + "=" + c_value;
}

Note: Change .mydomain.com according to your domain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM