Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Counter

Status
Not open for further replies.

Extension

Programmer
Nov 3, 2004
311
CA
Hi,

I'm currently working on a counter to log the visitors location (country). I'm using the ip-to-country service from api.hostip.com.

I simply want to store the stats in a flat file in the following structure:
Code:
United States (US)|34
Canada (CA)|10

My current code is working fine to get the country but my piece of code to update the file is wrong and doesn't work.
Here's my current code (with comments)
Code:
<?php

	// PROCESS
	// Reset country variable
	$country = '';
	
	// Get visitor's IP address
	$IP = $_SERVER['REMOTE_ADDR'];
	
	// If IP address exists
	// Get country (and City) via  api.hostip.info
	if (!empty($IP)) {
		$country = file_get_contents('[URL unfurl="true"]http://api.hostip.info/get_html.php?ip='.$IP);[/URL]
		// Reformat the data returned (Keep only country and country abbr.
		list ($_country) = explode ("\n", $country);
		$_country = str_replace("Country: ", "", $_country);
	}	
	
	// The visitor's country is now stored in $_country

	// UPDATE FILE
	
	// Stats file ( Following stucture: COUNTRY|COUNTS )
	$lines = file('stats.txt');
	
	// Reset variables
	$i = 0;
	$found = 0;
	
	// Loop through the stats.txt file
	
	foreach ($lines as $thisline) {
		$thisline = chop($thisline);
		list($countryDB, $countDB) = explode("|",$thisline);
	
		// If country is already in the stats.txt file; add a count
		if ($countryDB == $_country) {
			$count++;
			$lines[$i] = $countryDB . "|" . $countDB+1 . "\n";
			$found = 1;
			break;
		}
		
		// If country doesn't exist in the stats.txt file; add the record
		else {
			$lines[$i] = $_country . "|" . "1" . "\n";
		
		}
	
	    $i++;
	}


	// Write the file
	$content = implode('', $lines);
	$fp = fopen('stats.txt',"wb") or die(" !!! ");
	fputs($fp,$content);
	fclose($fp);

?>
 
assuming your code for retrieving $country and $city works ok, this is what you need. You do not need to read the data in first. just use the a+ mode to append the information to you stats file.

Code:
$fh = fopen($statsfile, "a+");
fwrite($fh, $country.'|'.$city."\r\n");
fclose($fh);
 
Thanks jpadie for your help.

The problem with my current piece of code is mainly with the loop where I need to define if the country already exists or is new.

 
i did not understand that from your original posting.
since text files are not inherently seekable you will have to read the entire file into memory and then rewrite it.

i've had a play with some replacement code but if you're using php5 then i'd really recommend using sqlite instead. using a database for this kind of thing makes life so much easier.

try this code

Code:
<?php
function upDateStatsFile($country){
	$countries = array();
	
	//open the file for reading
	$fh = fopen('statsfile.txt', "rbt");
	
	//iterate through the file. line by line
	while (!feof($fh)) {
		$_row = trim(fgets($handle, 1024));
		
		//test for a valid line
		if (!empty($_row)){
		
			//create some temp vars to hold the data
			list ($_country, $_count) = explode("|", $_row);
			
			//cleanse the data
			$_country = trim(strtolower($_country));
			$_count = (int) $_count;

			//make a comparison
			if (	$_country == trim(strtolower($country))	){
				$_count++;
			}

			//store the cleansed and updated data in a temporary array

			$countries[$_country] = $_count;
		}
	}
	fclose($fh);
	//reopen the statsfile for writing
	$fh = fopen('statsfile.txt', 'wbt');
	//iterate through the temp variable and write the data
	foreach ($countries as $country=>$count){
		fwrite($fh, "{$country}|{$count}\r\n");
	}
	fclose($fh);
}
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top