我正在尝试通过检查 ip 在带有过滤器的代码点火器中制作一个访问计数器。这是我到目前为止的代码:
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) 中的日期字段是日期时间。希望有人能帮助我!,谢谢!
我回答自己:
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);
}
}
谢谢!