Welcome Guest, Not a member yet? Register   Sign In
hit counter
#1

[eluser]rinaldi[/eluser]
hello my name is rizki.this is the first time i use this aplication. i need help.. how to make a script dynamic hit counter. so i can change the value that hit counter... on the update form hit counter...please help me
#2

[eluser]Pascal Kriete[/eluser]
So what you want is a simple page hit counter? Pretty simple really. All you need is a mechanism to avoid duplicates. It's easiest to just track ip addresses.

Code:
/* Hits table has an auto-incrementing id and an ip field */

// Grab client IP
$ip = $_SERVER['REMOTE_ADDR'];

// Check for previous visits
$query = $this->db->get_where('hits', array('ip' => $ip), 1, 0);
$query = $query->row_array();

if (count($query < 1) )
{
    // Never visited - add
    $this->db->insert('hits', array('ip' => $ip) );
}

To display the counter, simply count all rows:
Code:
echo $this->db->count_all('hits');

Welcome to CodeIgniter.
#3

[eluser]matthewr[/eluser]
Good clean code!
#4

[eluser]Seppo[/eluser]
Instead of $_SERVER['REMOTE_ADDR'] you can use $this->input->ip_address();
It's not much, but it's CI-way
#5

[eluser]rinaldi[/eluser]
so i must use database... to save that value... n that code i must put in controlled right. and how the view page? just call the variable right? ohh n i wanna ask you all how to make a code using post form or just like simple input data... but not using scaffolding
#6

[eluser]xwero[/eluser]
Little snippet code error
Code:
if (count($query < 1) )
should be
Code:
if (count($query) < 1 )
#7

[eluser]rinaldi[/eluser]
ok thank you..
#8

[eluser]Bramme[/eluser]
gotta bump this for a second: is there a way to do this without using a database? because I gotta track hits on two tables (news/blog and tutorials), so I would allready need two tables: news/blog hits (with an extra field including the id of the news/blog item) and a table tut hits (with again the extra field for the tutorial id). Now this site gets a pretty high visitor count, so those two tables will get a lot of rows quick. Or is this no problem?




Theme © iAndrew 2016 - Forum software by © MyBB