【问题标题】:why can not read cookie setted by javascript with php on explorer?为什么无法在资源管理器上使用 php 读取 javascript 设置的 cookie?
【发布时间】:2013-04-10 03:17:50
【问题描述】:

我有一个问题,我需要帮助。我正在做一些简单的步骤:

  1. 我用 php 设置了一个 cookie(如果尚未设置)。
  2. 我用 php 再次读取了这个 cookie,它运行良好(适用于所有浏览器!)。
  3. 我重置了之前设置的 cookie,这次使用的是 javascript。
  4. 我用 php 读取了 javascript 设置的 cookie,在 firefox、chrome 上它工作正常,但在 explorer、opera、safari(我使用最新版本)上它不起作用。无法读取 cookie。没有错误返回,但 cookie 的字段为空白。请看下面的代码。

php代码:

<?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 代码:

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=/";
  }

再次,我在所有情况下都使用 php 读取 cookie,但是当我使用 php 创建它时,它可以被所有浏览器读取,当我使用 javascript 创建它时,它可以被 firefox、chrome 读取,但不能在资源管理器上读取、歌剧和野生动物园。

注意1:没有语法错误。

注意2:浏览器是最新版本。

请帮忙。提前谢谢你!

【问题讨论】:

  • 您是否检查过 valueSerialized 在这些浏览器中包含您的数组。也许您需要引用数组的键??
  • @BassJobsen 如果我们谈论的是相同的想法,我已经这样做了,并且我认为 javascript 以与 php 不同的方式序列化 cookie 的值。但同样的想法也发生在 Firefox 和 chrome 中,但它就像我说的那样工作。有进一步的说明吗?
  • 写入cookie的大部分函数,​​如w3schools.com/js/js_cookies.asp在值上使用escape()

标签: php javascript internet-explorer cookies opera


【解决方案1】:

试试看,

改变这个

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

有了这个

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

在任何浏览器上使用print_r($_COOKIE) 来查看差异。

【讨论】:

  • 感谢您的回答,但问题不在于 if 语句,而在于 javascript 如何对 cookie 中的数据进行编码。我用 Bass Jobsen 的建议解决了这个问题,使用了像 w3schools.com/js/js_cookies.asp 这样的 escape()。
猜你喜欢
  • 2014-11-26
  • 2015-05-19
  • 2021-06-17
  • 1970-01-01
  • 1970-01-01
  • 2015-10-18
  • 1970-01-01
  • 2012-11-20
  • 1970-01-01
相关资源
最近更新 更多