1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| <?php
$compteurId = $_POST['compteurId'];
if(!file_exists("./ip/".$compteurId.".txt"))
{
$f = fopen("./ip/".$compteurId.".txt", "x+");
fclose($f);
}
$str = file_get_contents("./ip/".$compteurId.".txt");
$line = explode("\n", $str);
$flag = true;
foreach($line as $val)
{
if($_SERVER['REMOTE_ADDR'] == $val)
{
$flag = false;
if(isset($_POST['compteurId']))
{
$compteurId = $_POST['compteurId'];
$fichier = './clickcount.data';
$lines = file($fichier);
foreach($lines as $index => $line)
{
$split = explode(";",$line); // séparation : nom_compteur;nombre_clic
if(isset($split[0]) && $split[0]==$compteurId) {
$nbr = $split[1];
$trouver = true;
}
if($trouver == true){
break;
}
}
if($trouver == true){
echo $nbr;
} else {
echo 'N/A';
}
}
}
}
if($flag)
{
$file = "./ip/".$compteurId.".txt";
$write = $_SERVER['REMOTE_ADDR']."\n";
$open = fopen($file, 'a');
fwrite($open, $write);
$nbr = 1;
$trouver = false;
if(isset($_POST['compteurId']))
{
$compteurId = $_POST['compteurId'];
$fichier = './clickcount.data';
$lines = file($fichier);
foreach($lines as $index => $line)
{
$split = explode(";",$line); // séparation : nom_compteur;nombre_clic
if(isset($split[0]) && $split[0]==$compteurId) {
// incrémentation du compteur (nombre de clics)
$nbr = $split[1]+1;
// on enregistre la nouvelle ligne
$lines[$index] = $split[0].';'.$nbr.PHP_EOL; // PHP_EOL = passage à la ligne
$trouver = true;
}
if($trouver == true){
break;
}
}
if($trouver == true){
// On ré-écrit le fichier
$newfile = implode('', $lines);
$new = fopen($fichier,'r+');
$ok = fwrite($new, $newfile);
// on affiche le compteur
echo $nbr;
} else {
echo 'N/A';
}
}
}
?> |