<?php



// Relative URL of text file that holds hit info:
$lf_name = "hits.txt";

// Save new log file each month
//   0 = No
//   1 = Yes
$monthly = 1;

// Path to store old files:
// Default for June, 2012:
//   oldfiles/6-12.txt
$monthly_path = "oldfiles";

$type = 2;

// Text to display
// before all hits.
$beforeAllText = "Hits: ";

// Before unique hits.
$beforeUniqueText = "Unique Visits: ";


// Only change this if you are recording both values.
// Separator for unique and all hits display - use HTML tags! (line break is default)
$separator = "<br \>";


$log_file = dirname(__FILE__) . '/' . $lf_name;

//Visit or IP.
	$uIP = $_SERVER['REMOTE_ADDR'];

	//Check for "hits.txt" file.
	// if (file_exists($log_file)) {
		//Check if today is first day of month
		// if (date('j') == 10) {
			//Ensure that monthly dir exists
			// if (!file_exists($monthly_path)) {
				mkdir($monthly_path);
			

			//Check if prev month log file exists already
			$prev_name = $monthly_path . '/' . date("n-Y", strtotime("-1 month"));
			//if (!file_exists($prev_name)) {
				//If not, move/rename current file
				copy($log_file, $prev_name);

				//Create new $toWrite based on CONFIG
				//Write file according to CONFIG above.
				if ($type == 0) {
					$toWrite = "1";
					$info = $beforeAllText . "1";
				} else if ($type == 1) {
					$toWrite = "1;" . $uIP . ",";
					$info = $beforeUniqueText . "1";
				} else if ($type == 2) {
					$toWrite = "1;1;" . $uIP . ",";
					$info = $beforeAllText . "1" . $separator . $beforeUniqueText . "1";
				}
				goto write_logfile;
			
		
	
	//if "hits.txt" doesn't exist, create it.
	$fp = fopen($log_file ,"w");
	fclose($fp);

		//Write file according to CONFIG above.
		if ($type == 0) {
			$toWrite = "1";
			$info = $beforeAllText . "1";
		} else if ($type == 1) {
			$toWrite = "1;" . $uIP . ",";
			$info = $beforeUniqueText . "1";
		} else if ($type == 2) {
			$toWrite = "1;1;" . $uIP . ",";
			$info = $beforeAllText . "1" . $separator . $beforeUniqueText . "1";
		}
	

		//Get contents of "hits.txt" file.
		$log = file_get_contents($log_file);


//Position of separators.
			$c1Pos = strpos($log, ";");
			$c2Pos = strrpos($log, ";");

			//Separate log file into regular hits, unique hits, and IPs.
			$pieces = explode(";", $log);
			$allHits = $pieces[0];
			$uniqueHits = $pieces[1];
			$IPs = $pieces[2];
			$IPArray = explode(",", $IPs);

			//Increase regular hits.
			$allHits = intval($allHits) + 1;

			//Search for visitor IP in list of IPs.
			if (array_search($uIP, $IPArray, true) === false) {
				//Increase ONLY unique hits and append IP.
				$uniqueHits = intval($uniqueHits) + 1;
				$toWrite = $allHits . ";" . $uniqueHits . ";" . $IPs . $uIP . ",";
			} else {
				//Else just include regular hits.
				$toWrite = $allHits . ";" . $uniqueHits . ";" . $IPs;
			}
			//Info to show.
			$info = $beforeAllText . $allHits . $separator . $beforeUniqueText . $uniqueHits;


$img_number = imagecreate(500,40);
$backcolor = imagecolorallocate($img_number,237,94,82);
$textcolor = imagecolorallocate($img_number,0,0,0);

imagefill($img_number,0,0,$backcolor);
$number = " Your IP is $_SERVER[REMOTE_ADDR]";

Imagestring($img_number,10,15,13,$info,$textcolor);

header("Content-type: image/jpeg");
imagejpeg($img_number);
?>