我正在尝试使用过滤器通过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)中的日期字段是日期时间。 希望有人能帮帮我!谢谢!
答案 0 :(得分: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);
}
}
谢谢!