4

1 つの問題があり、助けが必要です。私はいくつかの簡単な手順を実行しています:

  1. php で Cookie を設定します (まだ設定されていない場合)。
  2. この Cookie を php で再度読み取ると、正常に動作します (すべてのブラウザーで!)。
  3. 今回はjavascriptで以前に設定したCookieをリセットしました。
  4. javascript によって設定された Cookie を php で読み取り、Firefox では正常に動作しますが、エクスプローラー、オペラ、サファリ (私は最新バージョンを使用しています) では動作しません。クッキーを読み取れません。エラーは返されませんが、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 では問題なく読み取ることができますが、エクスプローラー、opera では読み取れません。サファリ。

注 1: 構文エラーはありません。

注2: ブラウザは最新版です。

助けてください。前もって感謝します!

4

1 に答える 1

1

それを試して、

これを変える

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

これとともに

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

またprint_r($_COOKIE)、どのブラウザーでも使用して違いを確認してください。

于 2013-04-18T05:39:58.670 に答える