0

我正在尝试通过检查 ip 在带有过滤器的 codeigniter 中创建访问计数器。到目前为止,这是我的代码:

public function new_visit($ip)
    {
        $new_ip = $ip;

        $this->db->from('visitas');
        $this->db->where('ip_addr', $new_ip);
        $query = $this->db->get();
        $count = $query->num_rows();

        if ($count > 0) {
            $lastv = $query->row('last_visit');
            $currentv = new DateTime('NOW');

            $consulta = $this->db->query('SELECT TIMESTAMPDIFF(HOUR,"$currentv","$lastv" ) FROM visitas WHERE ip_addr = "$new_ip"',FALSE);

            **if ($consulta->result() > 10) {**
                $this->db->set('cant_visit', 'cant_visit+1', FALSE);
                $this->db->set('last_visit', 'NOW()', FALSE);
                $this->db->update('visitas');
            }

        } else {
            $data = array(
                'ip_addr'=>$new_ip
            );
            $this->db->set('cant_visit', 'cant_visit+1', FALSE);
            $this->db->set('first_visit', 'NOW()', FALSE);
            $this->db->insert('visitas', $data);
        }

我的问题是,突出显示的扇区总是返回 true,所以每次单击菜单栏时我总是会获得新的访问。我尝试了不同的方法来做到这一点,但到目前为止没有成功。我的 sql 表 (cant_visit) 中的日期字段是一个日期时间。希望有人可以帮助我!,谢谢!

4

1 回答 1

0

我自己回答:

public function new_visit($ip)
 {
  $new_ip = $ip;

  $this->db->from('visitas');
  $this->db->where('ip_addr', $new_ip);
  $query = $this->db->get();
  $count = $query->num_rows();

  if ($count > 0) {
   $currentv = new DateTime('NOW');
   $currentv = $currentv->format('Y-m-d H:i:s'); // had to format this

   $q = $this->db->query("SELECT TIMESTAMPDIFF(HOUR, '$currentv', last_visit) as timediff FROM visitas WHERE ip_addr = '$new_ip'");

   $time_diff = $q->row()->timediff;  
   // return $time_diff; <-- just for testing

   if ($time_diff > 10) { 
    $this->db->set('cant_visit', 'cant_visit+1', FALSE);
    $this->db->set('last_visit', 'NOW()', FALSE);
    $this->db->update('visitas');
   }

  } else {
   $data = array(
    'ip_addr'=>$new_ip
   );
   $this->db->set('cant_visit', 'cant_visit+1', FALSE);
   $this->db->set('first_visit', 'NOW()', FALSE);
   $this->db->insert('visitas', $data);
  }
 }

谢谢!

于 2013-09-18T14:44:01.637 回答