简体   繁体   English

php无法识别javascript cookie

[英]php cannot recognize javascript cookie

I know there is no difference between JavaScript cookies and PHP cookies. 我知道JavaScript cookie和PHP cookie之间没有区别。 yet. 然而。 I'm setting a cookie with JavaScript, and checking it with PHP. 我正在使用JavaScript设置cookie,并使用PHP对其进行检查。

When I check the cookie with JavaScript, it returns as set. 当我使用JavaScript检查cookie时,它将按设置返回。 But when I check in PHP, it returns as not set. 但是当我签入PHP时,它返回未设置的状态。 Here's my code: 这是我的代码:

Javascript Java脚本

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 的HTML

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

PHP 的PHP

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

When I recall the moo moo() script. 当我回想起moo moo()脚本时。 It alerts with my name 它用我的名字提醒

When I load the PHP script it says "moo moo didn't work". 当我加载PHP脚本时,它说“ moo moo没用”。

THE FIX!: 修复!:

Thank you for the suggestions everyone. 谢谢大家的建议。 Alas, the solution was much simpler and as usual, a stupid error. las,解决方案要简单得多,而且像往常一样是一个愚蠢的错误。 Originally the function moo moo() read: 最初,函数moo moo()读取为:

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". 因此,我第一次调用该函数时会设置一个名为“ username”的cookie。 Then i changed it to how it is now. 然后我将其更改为现在的状态。 So since username was already set, it didn't set moomoo. 因此,由于已经设置了用户名,因此没有设置moomoo。 So the php function couldn't find moomoo cause it was never set. 因此php函数找不到moomoo,因为它从未设置过。 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. 看来没有在JavaScript中设置moomoo变量。 If your name is alerted, the else block isn't being executed. 如果您的名字被警告,则else块不会被执行。

Try checking your username cookie in php, and it will display. 尝试在php中检查您的用户名cookie,它将显示。

Try to set the Domain and Path for the cookie. 尝试设置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. 注意:根据您的域更改.mydomain.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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