How to calculate page views in Codeigniter and limit count by IP address?

hairynuggets picture hairynuggets · May 8, 2012 · Viewed 7.1k times · Source

I am looking to calculate how many times people have viewed my users profiles on my site.

I would like to keep a count in my database and to stop the count incrementing when a user refreshes the page. Limit by IP.

I understand that a cache of ip addresses would need to be created and emptied on a daily basis.

Is there any instruction on how to do this. Can anyone talk me through it?

Answer

Adam picture Adam · May 8, 2012
  1. You can use $this->input->ip_address() to take the user's ip address in the controller
  2. In the database you save the ip, the time the user first visited the site and a counter
  3. If the time is less than 24 hours, do not increment the counter.
  4. If the time is greater than 24 hours update the time for the ip and update the counter.
  5. Get the counter: $this->db->select_sum("counter")->get("views_table"); and process the result.