剛才有同學在群裡詢問:$_COOKIE 的時間是多長,他指的是“我直接用 $_COOKIE存取的”,也就是說用$_COOKIE這個全局變量儲存一個值。那麼這個值會存在多長時間,而不是用setcookie來指定。
那麼這個值到底是儲存多長時間呢?在PHP手冊上面查詢,沒有找到結果,最後發現是在php.ini裡指定的。
; Lifetime in seconds of cookie or, if 0, until browser is restarted.
session.cookie_lifetime = 0
; The path for which the cookie is valid.
session.cookie_path = /
; The domain for which the cookie is valid.
session.cookie_domain =
; Whether or not to add the httpOnly flag to the cookie, which makes it inaccessible to browser scripting languages such as JavaScript.
session.cookie_httponly =
php.ini裡面可以設定session.cookie_lifetime這個值,即預設cookie儲存多少秒,如果為0的話那麼就和浏覽器程序是相同的。
$coo = 'xxx';
$_COOKIE['xxx'] = $coo;
var_dump($_COOKIE);
?>
結果為 array(2) { ["ZDEDebuggerPresent"]=> string(14) “php,phtml,php3″ ["xxx"]=> string(3) “xxx” }
而如果我把代碼改為如下内容
var_dump($_COOKIE);
?>
重新整理浏覽器,結果為:array(1) { ["ZDEDebuggerPresent"]=> string(14) “php,phtml,php3″ }
$_COOKIE預設的值由php.ini中的session.cookie_lifetime指定。