douqun1977 2013-04-17 11:22
浏览 112

为什么不能在浏览器上阅读javascript与php设置的cookie?

I have one problem and I need help. I am doing some simple steps:

  1. i set a cookie with php(if it is not already set).
  2. I read this cookie again with php and it works fine (to all browsers!).
  3. I reset the cookie that i set previously, with javascript this time.
  4. I read the cookie setted by javascript with php and on firefox, chrome it works fine, but on explorer, opera, safari (i use the latest versions) it doesn't work. cookie can't be read. no error return but the field of the cookie are blank. plz see the code below.

php code:

<?php
    if (isset($_COOKIE["parameters"])){
            $UserParam = json_decode($_COOKIE["parameters"],true);
            print_r($UserParam); // when the cookie is set by php it prints the array (on all browsers), but when cookie is set by javascript nothing print on explorer,opera,safari.
            echo"<script>function readCookie(){ alert(\"the cookie was there with radius: ".$UserParam['radius']." type: ".$UserParam['type']."\"); //again when the cookie set with php the variables have values on every browser, but when the cookie set with javascript no values to the variables.
                            getLocationFunction(\"".$UserParam["radius"]."\",\"".$UserParam["type"]."\",\"".$UserParam["date"]."\",\"".$UserParam["name"]."\")}</script>";
    }
    else { 
            $defaultParam = array("radius" => "ως 50km","type" => "all","date" => "unlimited", "name" => "all");
            $defaultParamSerialized = json_encode($defaultParam);
            setcookie("parameters","$defaultParamSerialized",time()+3600);
            echo"<script>function readCookie(){ alert(\"there was no cookie so i set it: radius ".$defaultParam["radius"]."\");
                             getLocationFunction(\"".$defaultParam["radius"]."\",\"".$defaultParam["type"]."\",\"".$defaultParam["date"]."\",\"".$defaultParam["name"]."\")
                        }
                </script>";
    }

?>

javascript code:

function renewCookie(){ 
    var myArray = {radius: radius, type: type, date: price , name: company };
    var valueSerialized = JSON.stringify(myArray);
    createCookie('parameters',valueSerialized,999);   
  }
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=/";
  }

Again, I read the cookie in all situation with php, but when I create it with php it can be read lovely to all browsers, when i create it with javascript it can be read fine to firefox, chrome but not on explorer, opera and safari.

note1: there is no syntax error.

note2: browsers are the latest versions.

plz help. thank you in advance!

  • 写回答

1条回答 默认 最新

  • dongsheng8158 2013-04-18 05:39
    关注

    Try that,

    Change this

    if (isset($_COOKIE["parameters"])){
    

    with this

    $COOKIE = isset($_COOKIE["parameters"]) ? $_COOKIE["parameters"] : "";
    

    And use print_r($_COOKIE) with any browsers to see the difference.

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器