【问题标题】:Read a Javascript cookie with PHP使用 PHP 读取 Javascript cookie
【发布时间】:2014-04-22 06:47:52
【问题描述】:

我有一些设置 cookie 的 javascript 代码,当我尝试使用 php 读取该 cookie 时,它​​说它是未定义的。

Notice: Undefined index: scorecount in C:\xampp\htdocs\Contest\score.php on line 44

我正在使用 xamp 在我的计算机上运行和测试 php,这可能是它没有读取 cookie 的原因吗?

这是我的代码

<!DOCTYPE html>
<html>
    <head>
        <title>PHP Written Test</title>
    </head>
    <body style="background-color:#ADD8E6; margin-left:25%;">
        <?php include 'variables.php';?>
        <?php include 'functions.php';?>
        <?php     
            session_start();   
            $x = 1;
            $y = 0;
            $score = 0;

            while ($x <= 20) {  
                $a = $_POST["question" . $x];
                $b = $_SESSION['correct'][$y];
                $c = $_SESSION['question'][$y];

                if (trim($a) == trim($b)){      
                    $score++;   
                } else {
                    echo "$c<br>Your answer was: $a<br>The correct answer is: $b<br><br>";  
                }

                $x++;
                $y++;
            }

            echo "$score/20";
            echo $_COOKIE["scorecount"]; 
    ?>
    </body>
</html>

我会发布一张图片来证明 cookie 设置正确,但我没有足够的代表。 当我查看我的网络浏览器中的所有 cookie 时,cookie“scorecount”就在那里并且设置正确,如果有人能告诉我我做错了什么,那将非常感谢!

设置cookie的我的JS

function getCookie(c_name)
{
   alert("Im in getCookie");
   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)
{
   alert("Im in setCookie " );
   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;
   alert("Cookie Set");
}

function checkCookie(end)
{
   alert("Im in checkCookie");
   document.getElementById('full').ondragstart = function noDrag() { return false; };
   var score=getCookie("scorecount");
if (score!=null && score!="")
  {
     if(end == 'yes')
     {

     score = score - 1;




    return score;

     alert("Your score is " + score);

    }
    else 
    {

     score=1;
  if (score!=null && score!="")
    {

       setCookie("scorecount",score);
       //alert("Your score is " + score);
    }
  }
}

function correct(pgnmbr)
{
   var val;


   val = getCookie('scorecount');
   val = parseInt(val)+ 1;
   setCookie('scorecount',val);
   location.href = loc();
   alert(val);

}
function correct2(pgnmbr)
{
   var val;


   val = getCookie('scorecount');
   val = parseInt(val)+ 1;
   setCookie('scorecount',val);
   location.href = loc2();


}

   var clicks = 0;
function wrong(pgnmbr)
{   

   clicks++;
 if(clicks < 3)
 {

   alert("wrong try again");

 }
 else
 {  
   alert("you have exceeded your amount of trys");


    location.href = loc();
}
}

document.getElementsByClassName('box1')[0].addEventListener('click', correct, false);

document.getElementsByClassName('bg')[0].addEventListener('click', wrong, false);

【问题讨论】:

  • 检查是否先通过 headers
  • 你能贴出你设置cookie的js代码吗?
  • @DaveCribbs 您发布的代码有很多不必要的空白,难以阅读。提出问题时,您应该始终发布格式良好的代码。

标签: javascript php cookies


【解决方案1】:

好的,我解决了我的问题,显然您只能读取在同一域上设置的 cookie...#Duh#Noobie 感谢各位的建议!

【讨论】:

    【解决方案2】:

    尝试通过 ajax 调用将 cookie 内容发送到要用于读取 cookie 的 php 函数,祝你好运

    【讨论】:

    • 这就是 OP 实际上在做的事情
    • 我不知道 ajax 是什么……这是我第一次使用 php,但据我了解,我拥有的代码应该可以工作……甚至我的教授也被这个难住了。
    猜你喜欢
    • 2014-04-19
    • 1970-01-01
    • 2015-07-13
    • 2010-11-24
    • 2011-02-23
    • 2015-10-18
    • 2016-02-16
    • 2012-11-20
    • 2011-10-02
    相关资源
    最近更新 更多