Suppose you want to have many counters in your page; let's say that one is for a public site, other is for the mailing lists, other is for whatever: you can use the 'page' parameter to personalize the counters you want to have.
If you are only interested in the use of the counter, you are done. If you want to make your own counter, please keep reading.
===== Things you must have =====
In order that the following code works properly, you have the following in your website:
* a running mysql database (I won't consume much resources of it, but I need a space to keep track of the sessions and the counter values)
* permission to run php in your web site (later we will go over this)
* the "gd" graphics libary installed in your system
==== Setting up the database ====
It is desirable to have a mysql database with a user with select, update, delete and insert permission over two tables. Here is the code you have to execute as root in order to create things properly:
create database freecounter; // or the name you want to give to this
use freecounter;
create table if not exists session
(site varchar(250),
page tinytext,
sessionid varchar(50),
creation datetime,
last datetime,
useragent tinytext,
ip varchar(50),
pages int(16));
create table if not exists counter
(site varchar(250),
page tinytext,
count int(16));
grant select, insert, delete, update
on freecounter.* to freecounter
identified by 'CHOOSE A PASSWORD HERE';
Check that you have done things well:
# mysql -u freecounter -p
password: CHOOSE A PASSWORD HERE
mysql> use freecounter;
mysql> select * from session;
mysql> select * from counter;
==== Test that php is running in your site ====
Create a file "hello.php" with the following:
And check that you see it correctly in a web browser. Let's say that your website is "www.example.com". So, to see this page you should type in a web browser the following: ''http://www.example.com/hello.php''. And you should see the message "hello world". If not, check things.
==== Check that GD library is installed and working ====
Edit again the "hello.php" and change by the following:
And press the "refresh" button of your browser or open a new one. You should see again the message "Hello world", but this time is a picture showing this message instead of a message (if you don't understand what I'm saying, probably you shouldn't have to continue reading some points ago).
==== And finally the code ====
And finally, the code. You have to create two pages, ''cont2.php'' and ''stats.php''. Yes, I could have an include page to put the functions, but I do not want to have all the things properly one: an opportunity to you to enhace things a bit. However, there are the source code:
=== cont2.php ===
= '%s' "
." and site = '%s' "
." and page = '%s'",
mysql_real_escape_string($ip),
mysql_real_escape_string($a_half_ago),
mysql_real_escape_string($site),
mysql_real_escape_string($page) );
$result = mysql_query( $query );
$row = mysql_fetch_assoc( $result );
if( $row )
{
$found = true;
} // $row
} // $row
if( $found )
{
$update = sprintf( "update session set pages = pages + 1, "
." useragent = '%s', "
." ip = '%s', "
." last = '%s' "
."where site = '%s' and page = '%s' and sessionid = '%s'" ,
mysql_real_escape_string( $useragent ),
mysql_real_escape_string( $ip ),
mysql_real_escape_string( $now ), //last
mysql_real_escape_string( $site ), // site
mysql_real_escape_string( $page ),
mysql_real_escape_string( $session ) ); //
$result = mysql_query( $update );
}
else
{
$insert = sprintf( "insert into session (site, page, sessionid, creation, last, useragent, ip, pages) "
." values( '%s', '%s', '%s', '%s', '%s', '%s', '%s', %s )",
mysql_real_escape_string( $site ), // site
mysql_real_escape_string( $page ),
mysql_real_escape_string( $session ), // sessionid
mysql_real_escape_string( $now ), // creation
mysql_real_escape_string( $now ), // last
mysql_real_escape_string( $useragent ), // useragent
mysql_real_escape_string( $ip ), // ip
mysql_real_escape_string( 1 ) );
$result = mysql_query( $insert );
set_counter_data( $conn, $site, $page );
}
} // set_session_data
?>
=== stats.php ===
Visitas | Paginas visitadas | Tiempo por visita | La última hora | \n\r"); echo("".$row_hour['visits']." | \n\r"); echo("".round($row_hour['avg_pages'],1)." | \n\r"); echo("".round($row_hour['avg_time_spent']/60,1)." | \n\r"); echo("\n\r"); echo("
La últimas 24 horas | \n\r"); echo("".$row_day['visits']." | \n\r"); echo("".round($row_day['avg_pages'],1)." | \n\r"); echo("".round($row_day['avg_time_spent']/60,1)." | \n\r"); echo("
".$month['name'][$i]." | \n\r"); echo("".$month['stats'][$i]['visits']." | \n\r"); echo("".round($month['stats'][$i]['avg_pages'],1)." | \n\r"); echo("".round($month['stats'][$i]['avg_time_spent']/60,1)." | \n\r"); echo("
En todo el año | \n\r"); echo("".$row_year['visits']." | \n\r"); echo("".round($row_year['avg_pages'],1)." | \n\r"); echo("".round($row_year['avg_time_spent']/60,1)." | \n\r"); echo("
And that's all. Enjoy, and if you think that this is useful, please comment out here or recommend to your friends and colleagues.
~~DISCUSSION~~