dounuo8797 2018-06-10 10:04
浏览 150
已采纳

strtotime()期望参数1是字符串,在codeigniter中给出的数组

I am assigning a token to user when he logs in, this token will be stored in database and will expire within 12 hours.

For this when the user logs in it will be shown in a view,

**my view is **

<a href=" <?php echo site_url('admin/checkval') ?> ">check valididty of token</a>

when he clicks on this,

My Controller

public function checkval(){

    $this->load->library('session');

    $goal = $this->session->userdata('user_id');
    $uid = $goal[0];
    $token = $goal[1];
    $this->load->model('loginmodel');

    $check_id = $this->loginmodel->token_valid($uid, $token);
    if($check_id){
      $data['query'] = $this->loginmodel->get_last_ten_entries();
       $this->load->view('admin/account', $data);
       print_r($data);
      echo "valid";
    }else {
        echo "invalid";
    }


 }

if his token is valid he will be shown list of users

model

public function token_valid($uid, $token){

  $responce = $this->db->query("SELECT expired_at FROM user_auth 
                                WHERE id = '$uid' AND token = '$token'");
  $date = strtotime($responce->result());
  echo "$date";
  $cdate = date("Y-m-d H:i:s");
  $cdate = strtotime($cdate);
  $interval = date_diff($date,$cdate);
  $hour = $interval->h;
  if(h<12){
    return TRUE;
  }else {
  return FALSE;  // code...
  }

}

database

In user_auth table i have,

id int(50),token varchar(500),expired_at datetime

but when i do this, the error occurs as follows,

strtotime() expects parameter 1 to be string, array given

date_diff() expects parameter 1 to be DateTimeInterface, boolean given

Trying to get property of non-object0

Use of undefined constant h - assumed 'h'

I think once the first error is resolved, others will go away. How do i do that? What am i doing wrong here? Thank you for your suggesions

  • 写回答

2条回答 默认 最新

  • dongwaner1367 2018-06-10 10:20
    关注

    Return a row() instead of result(), your model should be like this :

    and date in date_diff() should be the DateTime objects from the date_create()

       public function token_valid($uid, $token)
       {
          $this->db->select('expired_at');
          $this->db->from('user_auth');
          $this->db->where('id',$uid);
          $this->db->where('token',$token);
          $query = $this->db->get();
          if ($query->num_rows() > 0)
          {
              $expired_date = $query->row()->expired_at;
    
              $expired_date = date_create($expired_date);
              $cdate = date_create('now');
              $interval = date_diff($expired_date,$cdate);
              $hour = $interval->h;
              if($hour < 12)
              {
                return TRUE;
              }else 
              {
                return FALSE;  
              }
          }   
       }
    

    For more : http://php.net/manual/en/datetime.diff.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据