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

令牌验证和到期

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 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";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料