2015-01-16 7 views
0

시간이 현재 시간 - 1 시간 미만인지 확인하려면 쿠키를 확인해야합니다. 그러나 현재 모델은 작동하지 않습니다. 기본적으로 쿠키를 모두 제거하기 전에 특정 간격 동안 쿠키를 몇 번 테스트합니다.시간이있는 쿠키 설정

<?php 

     if (isset($_COOKIE['cookieTest'])) { 
      if ($_COOKIE['cookieTest'] < (time() - (60*60))) { 
       $content = '<h2 style="color:green;font-weight:bold;">Cookie set a minute again.</h2>'; 
       unset($_COOKIE['cookieTest']); 
      } else { 
       $content = '<h3 style="color:red;">Hasn\'t been a minute!</h3>'; 
      } 
     } else { 

      setcookie('cookieTest', 
         time(), 
         (time()+3600), 
         '/', 
         'bfxsocial.strangled.net' 
        ) or die('<!DOCTYPE html> 
           <html lang="en"> 
            <head> 
             <title>Test Cookie</title> 
            </head> 
            <body> 
             <h3 style="color:red;">Cookie failed to be set</h3> 
            </body> 
          </html>'); 

      $content = '<h3 style="color:orange;">Cookie just set</h3>'; 

     } 
    ?> 
    <!DOCTYPE html> 
    <html lang="en"> 
     <head> 
      <title>Test Cookie</title> 
     </head> 
     <body> 
      <?php echo $content; ?> 
     </body> 
    </html> 

답변

0

나는 최종 목표는 무엇인지 확실하지 않다 또는 당신이 달성 하려는지,하지만 어쩌면이 도움이 될 것입니다

<?php 

date_default_timezone_set('America/Chicago'); 

$time = $_COOKIE['cookieTest']; // Timestamp to test 
$t = '1 hour'; // Length in the past 

if($time < strtotime("- $t")) 
{ 
    //Timestamp is older than $t. 
} 
else 
{ 
    //Timestamp is NOT older than $t. 
}