为什么不能在浏览器上用PHP读取javascript设置的cookie ?


why can not read cookie setted by javascript with php on explorer?

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

  1. 使用php设置cookie(如果没有设置)。
  2. 我用php再次阅读了这个cookie,它工作得很好(对所有浏览器!)。
  3. 我重置了我之前设置的cookie,这次使用javascript。
  4. 我读了javascript设置的cookie与php和firefox, chrome它工作得很好,但在浏览器,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=/";
  }

再次,我读cookie在所有情况下与php,但当我用php创建它可以阅读可爱的所有浏览器,当我用javascript创建它可以阅读火狐,chrome但不是在浏览器,opera和safari。

note1:没有语法错误。

note2:浏览器是最新版本。

请帮助。提前感谢!

试试,

改变这

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

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

和使用print_r($_COOKIE)在任何浏览器看到的区别