0

Hello i'm trying to make a post count function based on the codeigniter framework and what i done so far works great looking by my side. The function is complete and everything works fine.

But i have a question here from some experts to tell me that this is the correct ussage of the function and non of the above will harm my page loading. The function is accesed by jQuery get function on every click on the post.

First i'm checking if there is an ip address and if the ip address date inserted is more then 24 hours if its more i'm deleting the current row and then checking again becuase of the previous select its still remembering the last ip address and inserting again with new datime.

And other question should i make cleanjob every week or similar for all ip addreses ?

Here is my code:

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);
        }

    }

Thanks, any suggestions will be appreciate.

3
  • Hey, I am just trying to understand what you are doing more, are you wanting to log unique views on your posts when they are read? Commented Jan 7, 2014 at 1:57
  • No, i'm making post views count function to count the post views but to stop incrementing the view value on refreshing the page or on multiple clicks. Commented Jan 7, 2014 at 1:59
  • Okay, let me suggest what you should do in an answer and what you are describing is a unique view... Commented Jan 7, 2014 at 2:00

1 Answer 1

1

Instead of doing lots of queries to increment unique views on your posts, you should use and set cookies and have a fallback method if cookies are not enabled.

$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);      

}
Sign up to request clarification or add additional context in comments.

4 Comments

Great idea. I will try this one and will post here the results. Thanks Harry
No problem let me know if you have any problems :)
Btw, i'm already using cookies for my login system Harry. Is this will be a problem ?
Nope, just make sure the cookies have different names, which i'm sure they will :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.