<?php
// ------------------------------------------------------------------------- //
// Graphique affichant les connexions //
// ------------------------------------------------------------------------- //
// Auteur: Stephane Eyskens <stephaneey@hotmail.com> //
// Web: http://membres.lycos.fr/stephaneey //
// ------------------------------------------------------------------------- //
/*
Plutôt que le traditionnel compteur, ce script génère un graphique affichant le
nombre de visiteurs s'étant connectés sur votre site, mois après mois.
Il nécessite la librairie Jpgraph 1.7 ainsi que GD 1.6 ou plus.
*/
/* Table à créer dans votre base de données
CREATE TABLE connexions (
ip_adr varchar(40) NOT NULL default '',
condate varchar(16) NOT NULL default ''
) TYPE=MyISAM;
Note: Cette table ne contient pas de cléprimaire puisque un utilisateur peut
se connecter plusieurs fois avec la même IP
*/
//Script 'show_connexions.php'
//Script calculant le nombre de connexions et générant le graphique
//-->Veillez à spécifier correctement le chemin des programmes de Jpgraph
include ("../jpgraph/src/jpgraph.php");
include ("../jpgraph/src/jpgraph_line.php");
include ("../jpgraph/src/jpgraph_bar.php");
//<------
$current_year=date('y');
$sqllink = mysql_connect('localhost','root','');
mysql_select_db("votre_db",$sqllink) or die("CONNEXION to MYSQL IMPOSSIBLE.</A>");
$res=mysql_query("select count(ip_adr),mid(condate,4,2) from connexions ".
"where mid(condate,9,2)='$current_year' ".
"group by ip_adr,mid(condate,4,2) order by mid(condate,4,2)");
//Calcul du nombre de connexions de l'année en cours pour chaque mois.
if($res){
//Initialisation du tableau
for($i=0;$i<=11;$i++)
$connexions[$i]=0;
for($i=0;$i<mysql_num_rows($res);$i++){
switch (mysql_result($res,$i,1)){
case "01":
$connexions[0]++;
break;
case "02":
$connexions[1]++;
break;
case "03":
$connexions[2]++;
break;
case "04":
$connexions[3]++;
break;
case "05":
$connexions[4]++;
break;
case "06":
$connexions[5]++;
break;
case "07":
$connexions[6]++;
break;
case "08":
$connexions[7]++;
break;
case "09":
$connexions[8]++;
break;
case "10":
$connexions[9]++;
break;
case "11":
$connexions[10]++;
break;
case "12":
$connexions[11]++;
break;
}
}
}
else{
print "Error when running query";
exit;
}
// Génération du graphique
$labelx=array(" Jan"," Feb"," Mar"," Apr"," May"," Jun"," Jul"," Aug",
" Sep"," Oct"," Nov"," Dec");
$graph = new graph(300, 100);
$graph->img->SetMargin(30, 10, 30, 30);
$graph->SetScale('textlin');
$graph->title->Set("Visites d'utilisateurs différents");
$graph->title->SetColor('blue');
$graph->xaxis->SetTickLabels($labelx);
$graph->xaxis->SetColor("blue");
$bar1=new BarPlot($connexions);
$bar1->value->Show();
$bar1->SetFillColor('red');
$graph->Add($bar1);
$line1 = new LinePlot($connexions);
$graph->Add($line1);
$graph->Stroke();
?>
<?php
//Exemple pour renseigner votre table connexions
$sqllink = mysql_connect('localhost','root','');
mysql_select_db("votre_db",$sqllink) or die("CONNEXION to MYSQL IMPOSSIBLE.</A>");
$ip = gethostbyaddr($REMOTE_ADDR);
$year = date("Y", time());
$month = date("m", time());
$day = date("d", time());
$hour = date("H", time());
$minutes = date("i", time());
$date="$day/$month/$year $hour:$minutes";
mysql_query("insert into connexions values ('$ip','$date')");
?>
<?php
//Dans la page où vous désirez afficher le graphique
include "show_connexions.php";
?>