Codeigniter后视图计数功能 -

时间:2014-01-07 01:49:00

标签: php jquery codeigniter count views

您好我正在尝试基于codeigniter框架制作一个帖子计数功能,到目前为止我所做的工作很有效。功能齐全,一切正常。

但是我在这里有一些专家的问题告诉我这是函数的正确消息,而上述不会损害我的页面加载。每次点击帖子都会通过jQuery get函数加入该函数。

首先我要检查是否有IP地址,如果插入的IP地址日期超过24小时,如果它更多我正在删除当前行然后再次检查因为前一个选择它还记得最后一个ip地址并再次插入新的datime。

还有其他问题我应该每周为所有ip addreses做清洁工作吗?

这是我的代码:

function show_count($post_id){  

        $ip = $this->input->ip_address();

            $this->db->select('ip_address,data');
            $this->db->from('post_views');
            $this->db->where('ip_address',$ip);     
            $query = $this->db->get();

                foreach ($query->result() as $row){
                    $ip_address =  $row->ip_address;                 
                    $data = $row->data;                
                }

        if(isset($ip_address) && time() >= strtotime($data) + 8640){

            $this->db->where('ip_address',$ip);
            $this->db->delete('post_views');
        }

            $this->db->select('ip_address');
            $this->db->from('post_views');
            $this->db->where('ip_address',$ip);     
            $query = $this->db->get();

                foreach ($query->result() as $row){
                    $ip_address_new =  $row->ip_address;                           
                }

        if(!isset($ip_address_new) && $ip_address_new == false){

            $date = new DateTime('now', new DateTimeZone('Europe/Skopje'));
            $this->db->set('views', 'views+ 1', false);
            $this->db->where('post_id',$post_id);
            $this->db->update('posts');

            $data = array(
                'ip_address'=>$ip,
                'data'=>$date->format("Y-m-d H:i:s")
                );

            $this->db->insert('post_views',$data);
        }

    }

谢谢,任何建议都会受到赞赏。

1 个答案:

答案 0 :(得分:1)

如果没有启用Cookie,您应该使用并设置Cookie并使用后备方法,而不是通过大量查询来增加帖子上的唯一视图。

$post_id = "???"
if(isset($_COOKIE['read_articles'])) {
  //grab the JSON encoded data from the cookie and decode into PHP Array
  $read_articles = json_decode($_COOKIE['read_articles'], true);
   if(isset($read_articles[$post_id]) AND $read_articles[$post_id] == 1) {
     //this post has already been read
   } else {

     //increment the post view count by 1 using update queries

     $read_articles[$post_id] = 1;
     //set the cookie again with the new article ID set to 1
     setcookie("read_articles",json_encode($read_articles),time()+60*60*24);  

   }

} else {

  //hasn't read an article in 24 hours?
  //increment the post view count
  $read_articles = Array(); 
  $read_articles[$post_id] = 1;
  setcookie("read_articles",json_encode($read_articles),time()+60*60*24);      

}
相关问题