2013-07-04 2 views
1

내 쿠키 만료 시간을보고 싶습니다.PHP에서 쿠키 만료 시간 찾기?

내 코드는 다음과 같다 :

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

하지만 내가 페이지를 새로 고침하고 때마다 쿠키를 만료 시간을보고 싶어요. 어떻게해야합니까?

답변

4

쿠키의 일부로 정보를 인코딩하지 않으면 쿠키 만료 시간을 얻을 수 없습니다 (이 정보가있는 브라우저는이를 보내지 않습니다). 예를 들어 :

$expiresOn = time() + (86400 * 7); 
setcookie('blockipCaptcha','yes;expires=' . $expiresOn, $expiresOn); 

은 그렇다하더라도, 누군가가 이론적으로는 쿠키의 내용도 암호 학적 HMAC하지 정말 "신뢰"값을 인증하지 않는 한, 그래서 쿠키의 내용을 조작 할 수있다.

서명하고 쿠키의 내용을 인증하는 방법의 예 : 당신은 쿠키의 내용을 다시 얻을 때

$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); 

밖으로 제거하고 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!'); 
}