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

时间:2022-08-23 10:33:39

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. 我用php设置了一个cookie(如果它还没有设置)。
  3. I read this cookie again with php and it works fine (to all browsers!).
  4. 我用php再次阅读这个cookie,它工作正常(对所有浏览器!)。
  5. I reset the cookie that i set previously, with javascript this time.
  6. 我重置了之前设置的cookie,这次使用javascript。
  7. 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.
  8. 我读了用javascript和php设置的cookie和firefox,chrome它工作正常,但在资源管理器,歌剧,safari(我使用最新版本)它不起作用。 cookie无法读取。没有错误返回,但cookie的字段是空白的。请参阅下面的代码。

php code:

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 code:

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

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.

再次,我在所有情况下都使用php阅读cookie,但是当我使用php创建它时,它可以被所有浏览器读取,当我用javascript创建它时可以很好地阅读firefox,chrome但不能在资源管理器,歌剧和苹果浏览器。

note1: there is no syntax error.

note1:没有语法错误。

note2: browsers are the latest versions.

note2:浏览器是最新版本。

plz help. thank you in advance!

请帮助。先谢谢你!

1 个解决方案

#1


0  

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.

并在任何浏览器中使用print_r($ _ COOKIE)来查看差异。

#1


0  

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.

并在任何浏览器中使用print_r($ _ COOKIE)来查看差异。