Hi,

I'm trying to use the CountryFromIP class to determine a users country from their IP. Anyway, I keep getting a parse error when trying to test it:

Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home/eastgate/public_html/bulldogs/teamstore/CountryFromIP.inc.php on line 11

Example.php:

<?php

require_once('CountryFromIP.inc.php');

$ip ='202.164.32.81';
//$ip ='210.25.55.2';

$object = new CountryFromIP();

$countryName =  $object->GetCountryName($ip);
$flagPath =  $object->ReturnFlagPath();

echo "<BR> <B>Country: </B>".$countryName;
echo "<BR> <B>Flag: </B> <img src=".$flagPath." border='0'>";
?>

CountryFromIP.php:

<?Php
/**
 * This class generates the country name and its flag from its IP address
 *
 *
 * @author Rochak Chauhan
 */

 class CountryFromIP {

 private $CountryIPDatabase = 'CountryIPDatabase.txt';
 private $ip = '';

/**
 * Function to validate IP ( please modify it according to your needs)
 *
 * @param $ip - string
 *
 * @return boolean
 */
public function ValdateIP($ip) {
	$ipArray = explode(',',$ip);

	if(count($ipArray) != 4) {
		echo "<font color='red' size='3'> <b>ERROR: </b> Invalid IP</font>";
		return false;
	}
	else {
		return true;
	}
}

/**
 * Function to return Country name from the IPDatabase
 *
 * @param $ip string
 * 
 * @return string - name of the country, false otherwise
 */
public function GetCountryName($ip) {
	$this->ip = $ip;
	$ip = sprintf("%u", ip2long($ip));

	$csvArray = file($this->CountryIPDatabase);

	for($i=0; $i<count($csvArray); $i++) {
		$arrayOfLine = explode(',', $csvArray[$i]);
		if($ip >= $arrayOfLine[0] && $ip <= $arrayOfLine[1] ) {
			return $countryName = $arrayOfLine[2];
		}
	}
	return false;
}

/**
 *  Function to return local path to Country's flag
 *
 * @param $ip - string
 *
 * @return string - local path to flag image
 */
public function ReturnFlagPath() {

	if($countryName = trim(ucwords(strtolower($this->GetCountryName($this->ip))) )) {
                    $countryName = str_replace(' ','%20',$countryName);
		return "flag/$countryName.gif";
	}
	else {
	    return false;
	}
}

 }
?>

I can't work out why it's breaking? Does anyone know why? Or does anyone know of another accurate method to determine a users country?

Thanks for any help,
~Oni.

    Hi Oni,

    I have just cut and paste your code into two files and tried to run it... All worked fine.

    I have got no errors at all.

    Cheers for now.

      Looks to me like perhaps you are trying to run it on a PHP4 installation? If so, the visibility keywords like "private" and "public" are undefined. To (possibly) get it to work, you would then need to remove those visibility declarations from your method (function) declarations, and change them to "var" for the class variables.

        ...but see the announcement in the General Help forum.

          Write a Reply...