du6jws6975 2014-04-21 21:54
浏览 112
已采纳

用PHP读取Javascript cookie

I have some javascript code that sets a cookie and when i try to read that cookie with php it says that it is undefined.

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

I am using xamp to run and test the php on my computer, could that be the reason why it is not reading the cookie?

Here is my code

<!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>

I would post a picture to prove that the cookie is set correctly but i dont have enough rep. When I look at all the cookies in my web browser the cookie "scorecount" is there and is set correctly, if someone could please tell me what I am doing wrong that would be great thanks!

My JS that sets the cookie

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);
  • 写回答

2条回答 默认 最新

  • dtvfxzq3802 2014-04-21 22:47
    关注

    Ok I solved my issue apparently you can only read a cookie that was set on the same domain... #Duh#Noobie Thanks for the suggestions though guys!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真