dongqie4233
dongqie4233
2017-04-06 01:17
浏览 38
已采纳

令牌验证和到期

I'm trying the set up a user registration system that creates a token that expires after 48 hours (2 days). I attempted to program this but to no avail. When I test the code I get this error

A PHP Error was encountered
Severity: Error
Message: Class 'TimeDate' not found
Filename: models/User_model.php
Line Number: 58

Below is the code

    public function isTokenValid($token)
{
    $q = $this->db->get_where('tokens', array('token' => $token), 1);        
    if($this->db->affected_rows() > 0){
        $row = $q->row();             

        $created = $row->created;
        $createdTS = strtotime($created);
        $expiredate = new TimeDate('Y-m-d'); 
        //$todayTS = strtotime($today);
        $expiredate->add(new DateInterval('P2D'));
        if($createdTS != $expiredate){
            return false;
        }

        $user_info = $this->getUserInfo($row->user_id);
        return $user_info;

    }else{
        return false;
    }

}  
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duansha8764
    duansha8764 2017-04-06 05:47
    已采纳

    you could also use strtotime()

    Assuming:

    // the timestamp the token was created
    $created = $row->created;
    

    Since the token will expire after 48 hours, we need to add 48 hours to date variable $created

    $expireDate = date("Y-m-d H:i:s", strtotime($created . " + 48 hours"));
    

    If you only want the date part and not the time, you can omit the H:i:s
    You can now compare the $expireDate to today's timestamp.

    $today = date("Y-m-d H:i:s");
    if (strtotime($today) > strtotime($expire)) {
        echo "expired";
    } else {
        echo "not yet";
    }
    
    点赞 评论

相关推荐