How to execute an If statement if a cookie date is longer then 7 days?

Discussion in 'PHP' started by tdd1984, Feb 22, 2012.

  1. #1
    I am storing cookies in our visitors browser. If the cookie is older then 7 days they should be redirected to another page. If the cookie is not older then 7 days they will continue going to the page they visited directly from the address bar. Below is an example of my code and where I am stuck at.

    if(!$cookie['redirect']){
    
    $today = date("m.d.y");
    setcookie("redirect",$today, time()+3600*1080);   
    
    }
    
    $cookiedate = $_COOKIE['redirect']; 
    
    if($cookiedate > )
    PHP:
    As you can see I stored the current (today's date) in the cookie. Now I am at the if statement. I am trying to figure out how I would compare the cookie value to today's date. Therefore if the cookie value date is older then 7 days it will redirect to another page...
     
    tdd1984, Feb 22, 2012 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    Can't (i think).

    Best to set 3 cookies that will combine 2 cookies with a hash

    cookie 1 - your info
    cookie 2 - time
    cookie 3 - hash of cookie 1 and 2 to validate them

    then you can easily use cookie 2 (if validated) to find out the time of the cookie!
     
    EricBruggema, Feb 22, 2012 IP
  3. tdd1984

    tdd1984 Well-Known Member

    Messages:
    2,357
    Likes Received:
    42
    Best Answers:
    0
    Trophy Points:
    150
    #3
    I done got it figured out...

    $today = date("m.d.y");   
    $cookie_date_check = $_COOKIE['redirect'];  
    
    if(!isset($cookie_date_check['redirect'])){
    
    $end_date = date("m.d.y",strtotime("+7 days"));  
    setcookie("redirect",$end_date, time()+3600*24*90);    
        
    }
    
    if(isset($cookie_date_check)){
        
        if($cookie_date_check < $today){
    
            header('Location: http://www.domain.com/?source_detail=55');  
    
            }
    }
    PHP:
     
    tdd1984, Feb 22, 2012 IP