<?php
/***************************************************************************
 *                           visitorsucounter.php
 *
 *                           --------------------
 *   begin                : saturday, 15 sept, 2007
 *   owner                : Qumbetlian
 *   email                : qumbetlian@yahoo.com
 *
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is a free script; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   Acest program este un script liber; il puteti redistribui si/sau modifica
 *   sub termenii Licentei Publice Generale asa cum este publicata de
 *   Fundatia Free Software; fie in versiunea a doua a licentei, sau 
 *   (la alegerea dvs.) oricarei versiuni ulterioare.
 *
 ***************************************************************************/

// If storing file vas not set, then set storing file.
// Daca fisierul de stocare nu a fost precizat, atunci fixeaza fisierul de stocare.
if(!isset($visitorsStoringFile))
	$visitorsStoringFile = "visitorscount.txt"; // File of data storing. || Fisierul de stocare a datelor.

// See if page name is provided. || Verificam daca este furnizat numele paginei.
if(isset($pagename)) {
	$cookie_ip = $_COOKIE[$pagename]; // Get cookie IP. || extragem IP-ul cookie-ului.
	$sFfp = fopen($visitorsStoringFile, 'r+');
	flock($sFfp, 3);
	// Read the file data storing line by line for finding the page name field.
	// Citim fisierul de stocare linie cu linie pentru gasirea cimpului cu numele paginei.
	while($buffer = fgets($sFfp)) {
		if(!strcmp($buffer, $pagename."\n")) {
			$sFtell = ftell($sFfp);
			$visitorscounter = fgets($sFfp);
		}
	}
	// See if cookie IP exist and set cookie if not.
	// Verificam daca exista IP-ul cookie-ului si daca nu, il fixam.
	if(!$cookie_ip) {
		setcookie($pagename, $_SERVER['REMOTE_ADDR'], time()+60*60*24*365);
		if(isset($visitorscounter)) {
			$visitorscounter = 1 + "$visitorscounter"; // Increasing counter. || incrementare contor.
			fseek($sFfp, $sFtell);
			fputs($sFfp, "$visitorscounter");
		}
	}
	// If coockie IP exist, then check if them coresponding with the client IP.
	// Daca exista IP-ul cookie-ului, atunci verificam daca acesta corespunde cu IP-ul clientului.
	elseif($cookie_ip != $_SERVER['REMOTE_ADDR']) {
		if(isset($visitorscounter)) {
			$visitorscounter = 1 + "$visitorscounter"; // Increasing counter. || incrementare contor.
			fseek($sFfp, $sFtell);
			fputs($sFfp, "$visitorscounter");
		}
	}
	// Daca $visitorscounter nu a fost fixat, nu a fost gasit campul cu numele paginii si atunci il scriem in fisier.
	// If $visitorscounter was not ben set then was not found page name field and the will write it to file.
	if(!isset($visitorscounter)) {
		$visitorscounter = 1;
		fwrite($sFfp, $pagename."\n");
		fputs($sFfp, "$visitorscounter"."        \n");
	}
	fclose($sFfp);
	// Showing counter visitors. || Afisare numar de vizitatori.
	echo $visitorscounter;
}
?>
