Counters
|
|
|
|
<?php /*
Counter Class to apply a counter of a web site # of visits
Copyright (C) 2004 Marco Simonetti
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
@author Marco Simonetti
@contact mars17@infinito.it
PLEASE LET ME KNOW WHEN YOU USE THIS SCRIPT, IF YOU FIND IT USEFUL AND SO ON...
IF SOMEONE'S GOT GOOD IDEAS TO IMPROVE IT FEEL FREE TO WORK ON THE CODE, BUT
DON'T DELETE THESE LINES, THANKS!!!
*/
/*
DB STRUCTURE:
db name: counter
TABLE STRUCTURE:
table name: counter
fields: id [int(10) auto_increment primary key]
page [varchar(128)]
visit [int(10)]
*/
/*
first we assigne the variables the parameters to connect to mysql server;
if you prefer you can use these parameters in an external file (better!!!)
in this case just include the file in the first line before the class
[use require_once("file-for-connection.inc") or
include_once("file-for-connection.inc")]
*/
$host = "localhost"; //here the IP address of the mysql server (for better
//security don't use localhost or 127.0.0.0);
$user = "user"; //here the name of the user to access mysql server $password = "mypwd"; //here the password of the above user
//we inizialize the connection to mysql server $conn = mysql_connect($host, $user, $password);
//starts the class "Contatore" class Contatore {
var $t; //the variable used to pass the # of the counter in the function leggi()
var $pagina; //name of the page we want to add the new visit
function controlla() //ckeck if the name of the page is registered in the db
{
//there's a record in the db with the name of the page?
$query = mysql_query("SELECT * FROM counter.counter WHERE page='{$this->pagina}'")
or die (@mysql_error());
//if no record, creates a new one
if (!mysql_num_rows($query))
{
$query_newpage = mysql_query("INSERT INTO counter.counter (page) VALUES
('{$this->pagina}')") or die (@mysql_error());
}
}
function leggi ()
{
//call the function controlla
$this->controlla();
//query to upadate the number of visit for the page
$query = mysql_query("SELECT visit FROM counter.counter WHERE
page='{$this->pagina}'") or die (@mysql_error());
list($count) = mysql_fetch_array($query);
//upload the count variable with the current # of visits + 1
$count++;
//passes the value to he variable $t so we can access it in the function aggiorna
$this->t = $count;
}
//MAIN FUNCTION OF THE CLASS*******************************************
function aggiorna ()
{
//first we call the leggi funcion to read the actual # of visit for the page
$this->leggi();
//if it'all ok we update the table counter with the new # of visit for the page
$query = mysql_query("UPDATE counter.counter SET visit='{$this->t}'
WHERE page='{$this->pagina}'") or die (@mysql_error());
}
function visualizza()
{
//selects name of pages and number of visits
$query = mysql_query("SELECT page, visit SUM(visit) as 'totale' FROM
counter.counter") or die (mysql_error());
//prints the table to display the # of visits for all the pages in the db
print ("<html><head><title>Visualizzatore visite ibssas.it</title></head>
<body style="font-family:Arial, Helvetica, sans-serif; font-size:12px">
<table width="40%" border="0" cellspacing="1" cellpadding="1">
<tr>
<td><strong>Pagina</strong></td>
<td><strong>Visite</strong></td>
</tr>
<tr>
<td colspan="2" height="1" bgcolor="#000000">
</tr>");
//we display n # of rows, one for each row of the table counter
while ($row = mysql_fetch_assoc($query))
{
print("<tr>
<td>".$row['page']."</td>
<td>".$row['visit']."</td>
</tr>
<tr>
<td colspan="2" height="1" bgcolor="#000000">
</tr>");
}
print ("<tr>
<td></td>
<td>".$row['totale']."</td>"
"<tr>");
print ("</table></body></html>");
}
}
/*
USAGE: paste the code below at the beginning of the page/s you want to register
the # of visits
require_once("path-to-counter-file/counter.php");
//starts the class
$c = new Contatore();
//passes the class the value of the page (=the name of the page we're opening);
$c->pagina = $_SERVER['REQUEST_URI'];
//calls the function aggiorna
$c->aggiorna();
*/
/*
DISPLAYING THE RESULTS OF THE VISITS FOR OUR WEB SITE: paste the code below
in a new page
require_once("path-to-counter-file/counter.php");
$c = new Contatore;
$c->visualizza();
THAT'S ALL FOLKS!!!
*/
/*
VERSION 2.0 TODO.
- statistics for the period (i.e. total amount of pages viewed in the
first four months of the year), it means that I have to add the date
of the visits...
- implementation of the function visualizza() in order to make it more
flexible;
- sure we want to update our counter each time a user click on the refresh
button in the browser?
*/
?>
|
|
|
Usage Example
|
$c = new Counter;
$c->page=$_SERVER['REQUEST_URI'];
$c->aggiorna();
|
|
|
Rate This Script
|
|
|
|