|
Home -> Manuali e Tutorials -> Guida PHP -> Counter (1/2)
Scarica il tutorial | Stampa il tutorial | Cerca nel sito
REALIZZARE UN COUNTER IN PHP
Vediamo come creare un counter di pagina avanzato con il supporto di un database ( ), chiamato appunto "andrea_db" (qui trovate il dump e quindi la struttura del database). Questo sarà costituito da 7 tabelle: todooconnectes, todoocount, todoocount_ip, annuale, uniche, totali, url. Le prime tre servono per gestire gli ip delle persone connesse al sito, ed il conteggio delle pagine viste al giorno e totali; mentre le altre servono per gestire i conteggi anno per anno e mese per mese.
Di sotto sono riportate le caratteristiche che vengono mostrate di default nel counter:
- Tot.Pg.Viste : Il totale delle pagine viste da quando il counter è online
- Tot.Uniche : Il totale delle visite uniche da quando il counter è online
- Oggi.Uniche : Il totale delle visite uniche odierne
- Oggi.Totali : Il totale delle pagine viste oggi
- Online : Utenti online al momento
Di sotto, invece, sono riportate le caratteristiche che vengono mostrate nella pagina di statistiche del counter:
- Riepilogo Annuale ultimi due anni : Confronto tra i totali delle pagine viste e degli accessi negli ultimi due anni
- Riepilogo Visite Uniche Corrente Anno : Dettaglio mese per mese degli accessi al sito
- Riepilogo Pag. Tot. Viste Corrente Anno : Dettaglio mese per mese delle pagine viste
- SITO di provenienza : Dettaglio degli url di provenienza al sito
Un esempio è visibile qui.
Il counter è costituito quindi da un file di configurazione, config.php, dove vengono settati i dati del database, todoocount.php, che printa il counter ed aggiorna i valori, ed il file graf.php, che printa le statistiche.
Il codice php del file todoocount.php è il seguente:
<html>
<head>
<style>
BODY {SCROLLBAR-FACE-COLOR: #dee3e7; SCROLLBAR-HIGHLIGHT-COLOR: #ffffff; SCROLLBAR-SHADOW-COLOR: #dee3e7; SCROLLBAR-3DLIGHT-COLOR: #d1d7dc; SCROLLBAR-ARROW-COLOR: #006699; SCROLLBAR-TRACK-COLOR: #efefef; SCROLLBAR-DARKSHADOW-COLOR: #98aab1; BACKGROUND-COLOR: #e5e5e5; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif;}
TD {
FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif;font-size:9px;
}
</style>
</head>
<body>
<?
include "config.php";
$db = mysql_connect("$dbserver", "$dbuser", "$dbpass");
mysql_select_db("$dbbase",$db);
$mm = date("m");
switch($mm)
{
case 01:
$mese = "gen";
break;
case 02:
$mese = "feb";
break;
case 03:
$mese = "mar";
break;
case 04:
$mese = "apr";
break;
case 05:
$mese = "mag";
break;
case 06:
$mese = "giu";
break;
case 07:
$mese = "lug";
break;
case 08:
$mese = "ago";
break;
case 09:
$mese = "sett";
break;
case 10:
$mese = "ott";
break;
case 11:
$mese = "nov";
break;
case 12:
$mese = "dic";
break;
}
// DATI TABELLA VISITE UNICHE:
$uni = mysql_query("SELECT * FROM uniche where id=1");
// DATI TABELLA VISITE TOTALI:
$toti = mysql_query("SELECT * FROM totali where id=1");
// DATI TABELLA VISITE ANNUALI:
$annu = mysql_query("SELECT * FROM annuale where id=1");
// DATI TABELLA url: i suoi dati nn vengono cancellati alla fine dell'anno
//echo("ser:".$_SERVER["HTTP_REFERER"]);
if($_SERVER["HTTP_REFERER"]==NULL || $_SERVER["HTTP_REFERER"]=="")
{
$altro = mysql_query("SELECT * FROM url WHERE id=1");
while($row=mysql_fetch_array($altro,MYSQL_ASSOC))
{
$ret[]=$row;
}
$num=$ret[0]["num"]+1;
$qq=mysql_query("UPDATE url SET num=".$num." WHERE id=1");
}
else
{
$c = explode("/",$_SERVER["HTTP_REFERER"]);
//echo $c[2];
// RICERCO NEL DB L'URL IN QUESTIONE
$qu="SELECT * FROM url where ref='".$c[2]."'";
$altro = mysql_query($qu,$db);
while($row=mysql_fetch_array($altro,MYSQL_ASSOC))
{
$ret[]=$row;
}
if($ret==null)
{
$query="INSERT INTO url (ref,num) VALUES('".$c[2]."',1);";
$qq=mysql_query($query);
}
else
{
$num=$ret[0]["num"]+1;
$qq=mysql_query("UPDATE url SET num=".$num." where id=".$ret[0]["id"]);
}
}
// DATI TABELLA COUNTER:
$req = mysql_query("SELECT * FROM todoocount where id=0");
$res = mysql_numrows($req);
$totali= mysql_result($req,0,"totali");
$tot_oggi= mysql_result($req,0,"tot_jour");
$uniche = mysql_result($req,0,"uniche");
$jour = mysql_result($req,0,"jour");
$date = mysql_result($req,0,"date");
$date2 = date("Y-m-d");
$ip=$_SERVER['REMOTE_ADDR'];
$anno_corr = date("Y");
$arr_date = explode("-",$date);
$anno_db = $arr_date[0];
if ($anno_corr != $anno_db)
{
// AZZERO TUTTI I VALORI AL CAMBIO DELL'ANNO:
mysql_query("UPDATE totali SET gen=0, feb=0, mar=0, apr=0, mag=0, giu=0, lug=0, ago=0, sett=0, ott=0, nov=0, dic=0, totali=0 where id=1");
mysql_query("UPDATE uniche SET gen=0, feb=0, mar=0, apr=0, mag=0, giu=0, lug=0, ago=0, sett=0, ott=0, nov=0, dic=0, totali=0 where id=1");
mysql_query("UPDATE annuale SET tot_scorso=totali, tot_uniche_scorso=tot_uniche, totali=0, tot_uniche=0 where id=1");
}
if ($date != $date2)
{
// AZZERO IL COUNTER ( solo valori giornalieri ):
mysql_query("UPDATE todoocount SET date='$date2', jour=0, tot_jour=0 where id=0");
mysql_query("delete from todoocount_ip");
mysql_query("DELETE FROM todooconnectes");
$jour=0;
$tot_oggi=0;
}
$req2 = mysql_query("SELECT * FROM todoocount_ip where ip like '$ip'");
$res2 = mysql_numrows($req2);
//--------- print_r($res2); $res2 è = 1 quando l'utente è gia entrato nel sito ----------//
echo("<TABLE border=0 cellSpacing=1 cellPadding=3 width='100%'><TR><TD> <A HREF='grafici/graf.php' target=void><IMG SRC='stat.gif' WIDTH=23 HEIGHT=26 BORDER=0 alt='Vedi statistiche' align=top></A> Online dal: <I>30/07/2004</I></TD></TR><TR><TD align=middle><BR><TABLE width='80%' border=0><TR><TD>");
/* totali : pg viste totali
uniche : visite uniche totali
jour : visite uniche giornaliere
tot_jour : pg viste giornaliere
*/
if($res2 == 1)
{
$t_g=$tot_oggi+1;
$tot=$totali+1;
mysql_query("UPDATE todoocount SET totali=totali+1, tot_jour=tot_jour+1 where id=0");
mysql_query("UPDATE totali SET totali=totali+1, ".$mese."=".$mese."+1 where id=1");
mysql_query("UPDATE annuale SET totali=totali+1 where id=1");
echo "<B>Tot.Pg.Viste :</B> ".$tot."<br><B>Tot.Uniche :</B> ".$uniche."<br><B>Oggi.Uniche :</B> ".$jour."<br><B>Oggi.Totali :</B> ".$t_g."<br>";
}
else
{
$t_g=$tot_oggi+1;
$tot=$totali+1;
$uni=$uniche+1;
$o=$jour+1;
mysql_query("INSERT INTO todoocount_ip (ip) VALUES ('$ip')");
mysql_query("UPDATE todoocount SET totali=totali+1, tot_jour=tot_jour+1, uniche=uniche+1, jour=jour+1 where id=0");
mysql_query("UPDATE uniche SET totali=totali+1, ".$mese."=".$mese."+1 where id=1");
mysql_query("UPDATE totali SET totali=totali+1, ".$mese."=".$mese."+1 where id=1");
mysql_query("UPDATE annuale SET totali=totali+1, tot_uniche=tot_uniche+1 where id=1");
echo "<B>Tot.Pg.Viste :</B> ".$tot."<br><B>Tot.Uniche : </B>".$uni."<br><B>Oggi.Uniche :</B> ".$o."<br><B>Oggi.Totali :</B> ".$t_g."<br>";
}
$limite = time() + $duree_estimee;
mysql_query("DELETE FROM todooconnectes WHERE ip='$ip' OR date<".time());
mysql_query("INSERT INTO todooconnectes (id,ip,date) VALUES ('0','$ip','$limite')");
$result = mysql_query("SELECT COUNT(id) FROM todooconnectes");
$row = mysql_fetch_row($result);
echo "<B>Online : </B>".$row[0]."";
echo "</TD></TR></TABLE><BR></TD></TR></TABLE><BR>";
mysql_close($db);
?>
</body>
</html>
|
Torna su | Indice Guida | Pagina << 30 >>
|