在PHP中查找Cookie到期时间?

在PHP中查找Cookie到期时间?,php,cookies,setcookie,Php,Cookies,Setcookie,我想看看我的饼干过期时间 我的代码是这样的: setcookie('blockipCaptcha','yes',time() + (86400 * 7)); 但是每当我刷新页面时,我都希望看到cookie的过期时间。如何做到这一点?除非您将该信息编码为cookie的一部分,否则无法获取cookie过期时间(拥有此信息的浏览器不会将其发送过来)。例如: $expiresOn = time() + (86400 * 7); setcookie('blockipCaptcha','yes;expir

我想看看我的饼干过期时间

我的代码是这样的:

setcookie('blockipCaptcha','yes',time() + (86400 * 7));

但是每当我刷新页面时,我都希望看到cookie的过期时间。如何做到这一点?

除非您将该信息编码为cookie的一部分,否则无法获取cookie过期时间(拥有此信息的浏览器不会将其发送过来)。例如:

$expiresOn = time() + (86400 * 7);
setcookie('blockipCaptcha','yes;expires=' . $expiresOn, $expiresOn);
即使这样,理论上也可能有人篡改cookie内容,因此您无法真正“信任”该值,除非cookie内容也通过密码验证

有关如何对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!');
}

除非将该信息编码为cookie的一部分(拥有此信息的浏览器不会将其发送),否则无法获取cookie过期时间。例如:

$expiresOn = time() + (86400 * 7);
setcookie('blockipCaptcha','yes;expires=' . $expiresOn, $expiresOn);
即使这样,理论上也可能有人篡改cookie内容,因此您无法真正“信任”该值,除非cookie内容也通过密码验证

有关如何对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!');
}