-1

何が間違っているのかわからない...

Cookie の設定に使用するページは次のとおりです: https://www.ni-dieu-ni-maitre.com/test.php

    $domain = "ni-dieu-ni-maitre.com";
$articleid = "test";

$lastviewedarticles = array();

if (isset($_COOKIE["viewed_articles"]) ) {
  $lastviewedarticles = unserialize($_COOKIE["viewed_articles"]);
}

if (!in_array($articleid, $lastviewedarticles)){
    $count = count($lastviewedarticles);
    if($count>=29)
        array_shift($lastviewedarticles);
    $lastviewedarticles[] = $articleid;
}
setcookie('viewed_articles', serialize($lastviewedarticles), time()+60*60*24*30, '/', '.' . $domain);

Then this page reads the cookie and output the content: https://www.ni-dieu-ni-maitre.com/test2.php

if ( isset($_COOKIE["viewed_articles"]) ) {
  $lastviewedarticles = unserialize($_COOKIE["viewed_articles"]);
}
echo "cookie is currently:<br>";
print_r($lastviewedarticles);

As you can see on the test pages, the cookies are always empty

4

2 に答える 2

1
... '.$domain');
    ^        ^

$domain単一引用符を使用します。これは、php が変数のそれぞれの値に置き換えられないことを意味します。

ドキュメント:

注: 二重引用符やヒアドキュメントの構文とは異なり、特殊文字の変数とエスケープ シーケンスは、単一引用符で囲まれた文字列内にある場合は展開されません。

また、Cookie から.$domainCookie にアクセスしないため、設定されていません。

ドキュメント:

ドメイン

Cookie を使用できるドメイン。ドメインを「www.example.com」に設定すると、Cookie が www サブドメインと上位のサブドメインで利用できるようになります。「example.com」などの下位ドメインで使用できる Cookie は、「www.example.com」などの上位サブドメインで使用できます。推奨されない » RFC 2109 をまだ実装している古いブラウザでは、先頭に . すべてのサブドメインに一致します。

次のように変更します。

... '.'.$domain);
于 2013-03-08T07:50:41.600 に答える
0

RFC 2109以降、domainパラメータは開始ドットで設定する必要があります(http://php.net/setcookie参照)。それを試してみてください、あなたのコードは大丈夫のようです。そして、から一重引用符を削除してください'.$domain.'

于 2013-03-08T07:53:20.187 に答える