[英]Finding Cookie Expiry Time in PHP?
我想看看我的cookie到期時間。
我的代碼是這樣的:
setcookie('blockipCaptcha','yes',time() + (86400 * 7));
但是每當我刷新頁面時,我都希望看到cookie的到期時間。 怎么做?
您無法獲得cookie到期時間,除非您將該信息編碼為cookie的一部分(瀏覽器,具有此信息但不發送)。 例如:
$expiresOn = time() + (86400 * 7);
setcookie('blockipCaptcha','yes;expires=' . $expiresOn, $expiresOn);
即便如此,有人理論上可以篡改cookie內容,因此除非cookie內容也通過HMAC加密認證,否則您無法真正“信任”該值。
如何簽名和驗證cookie內容的示例:
$secretKey = ''; // this must be a per-user secret key stored in your database
$expiresOn = time() + (86400 * 7);
$contents = 'yes;expires=' . $expiresOn;
$contents = $contents . ';hmac='. hash_hmac('sha256', $contents, $secretKey);
當您取回cookie的內容時,刪除並驗證HMAC部分:
$contents = $_COOKIE['blockipCaptcha'];
// I 'm doing this slightly hacky for convenience
list ($contents, $hmac) = explode(';hmac=', $contents);
if ($hmac !== hash_hmac('sha256', $contents, $secretKey)) {
die('Someone tampered with the contents of the cookie!');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.