0

クッキーを使用してログイン ID # を保存する単純なログイン設定があり、この ID を使用して、ユーザー名やその他の情報を表示できます。

ログアウトするために、次の php スクリプトを実行してログアウトします。

<?php
if (isset($_COOKIE['id'])) {
setcookie("id","",1);
}
header("Location: redirectpage.html");
exit;
?>

これは基本的にCookieを期限切れにします。ただし、リダイレクト ページに到達すると、次のコードが表示されます。

If (isset($_COOKIE['id'])) {
//display "You are logged in already"
} else {
// show login form
}

まだログインしていると表示され、別のページに移動してもまだログインしていると表示され、Cookie の値を表示すると実際の値が表示されます。つまり、Cookie の有効期限が切れていません。ここで何かが欠けているに違いありませんが、Cookie の有効期限が切れていないのはなぜですか?

注: 有効期限を time()-60 または何らかの値から 1 に変更しましたが、これは何も変更しませんでした。また、if ステートメントを削除して、php コードが実行されるたびに Cookie を期限切れにするだけでは、うまくいきません。

4

2 に答える 2

0

if(!empty($_COOKIE... isset() は空の文字列が設定されていると見なすため、使用してみてください。次の表を参照してください: http://docs.php.net/manual/en/types.comparisons.php

于 2012-08-07T10:38:35.420 に答える
0

Make sure that you are trying to delete the cookie using the same parameters you used to create it. From the php manual, http://gr2.php.net/setcookie, under "common pitfalls":

Cookies must be deleted with the same parameters as they were set with. If the value argument is an empty string, or FALSE, and all other arguments match a previous call to setcookie, then the cookie with the specified name will be deleted from the remote client. This is internally achieved by setting value to 'deleted' and expiration time to one year in past.

于 2012-08-07T15:06:33.087 に答える