News :
|
count_visits
Ce script php permet de gérer un compteur de visites internet.
Utilisation : Intégrer dans un script php un appel à la fonction count_visits() :
count_visits(<count_id>);où <count_id> est une chaîne de caractères pour identifier de façon unique
le compteur. Ce dernier est incrémenté si :
/count/count.dat doit être préalablement créé. Il contiendra les
statistiques de chaque compteur de la forme :
count_id:nb_visits:date:last_ip Télécharger : [count_visits.php]<?php function create_line($id, $separator, $count, $clockd, $clockh, $addr) { return $id.$separator.$count.$separator.$clockd.'-'.$clockh.$separator.$addr."\n"; } function compteur($id) { $count_file = $_SERVER['DOCUMENT_ROOT']."/count/count.dat"; $addr = $_SERVER['REMOTE_ADDR']; $clockd = date('Ymd'); $clockh = date('H'); $separator = ':'; $min_delay = 2; //echo "<br /><h4>Count infos: ".$id."</h4>"; //echo "Date: ".$clockd."-".$clockh; $fd = fopen($count_file, "r"); if ( $fd == FALSE ) return 0; /* Get all count lines */ $n = 0; $count = array(); while(!feof($fd)) { $line = fgets($fd, 4096); $count[] = $line; $n++; } fclose($fd); $n--; unset($count[$n]); //echo "<br />DEBUG: ".$n." différent ids have been read."; $id_found = FALSE; $same_ip = FALSE; for ($i=0; $i<$n; $i++) { $line = $count[$i]; $cur_id = strtok($line, $separator); if ( $cur_id == $id ) { /* id found */ $id_found = TRUE; $cur_count = strtok($separator); $cur_clockd = strtok('-'); $cur_clockh = strtok($separator); $cur_addr = trim(strtok($separator)); //echo "<br />The good is found."; if ( $addr != "127.0.0.1" && /* local ip */ $addr != "X.X.X.X") /* personal ip */ { if ( strcmp($cur_addr, $addr) == 0 ) { /* same ip */ //echo "<br />DEBUG: The ip adress is the identical: ".$addr; $same_ip = TRUE; } else { /* different ip */ //echo "<br />DEBUG: The ip adress is different: ".var_dump($addr)." vs ".var_dump($cur_addr); $same_ip = FALSE; } $delay = $clockd - $cur_clockd; //echo "<br />DEBUG: Delay in days: ".$delay; if ( $delay < 0 ) return 0; else if ( $delay > 1 ) { $delay = 24; } else if ($delay == 1) { $delay = 24 + $clockh - $cur_clockh; } else { $delay = $clockh - $cur_clockh; } if ( $same_ip == TRUE && $delay <= 0 ) { //echo "<br />DEBUG: The counter does not change."; $change = FALSE; } else if ( $same_ip == TRUE && $delay <= 2 ) /* update time, but no counter */ $change = TRUE; else { /* different ip or time > min, update count and time */ $change = TRUE; $cur_count++; } if ( $change == TRUE ) { //echo "<br />DEBUG: The counter is changing."; $count[$i] = create_line($cur_id, $separator, $cur_count, $clockd, $clockh, $addr); } } break; } } if ( $id_found == FALSE ) { /* id not found, add a new line */ $fd = fopen($count_file, "a"); if ( $fd == FALSE ) return 0; fwrite($fd, create_line($id, $separator, "1", $clockd, $clockh, $addr)); fclose($fd); $cur_count = 1; } else { /* increase number */ if ( $change == TRUE ) { $fd = fopen($count_file, "w"); for ($i=0; $i<$n; $i++) { fwrite($fd, $count[$i]); } fclose($fd); } } unset($count); return $cur_count; } ?> |