Hey guys,

Can anyone please help me with the code, I am trying to find the error, but no luck. Everything is coded properly. Server is running on php 5.3.8, MySQL 5.1

When I try to execute my php file, it gives me an error : Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION in /home/user/public_html/domainname.com/search/includes/ip.php on line 11

Here is the code :

<?php

define('SMALL', 0);
define('BIG',   1);

class ClientInfo {

	var $flag_dirs = array(SMALL => 'assets/flags/small', BIG => 'assets/flags/big'); 
	var $flag_ext  = 'png'; 

	cfunction getctrybycode($code) {
		$countryArray = array();
		$input = "includes/countries.dat";
		$fd = fopen($input,"r") or die("Error: cannot open $input!");
		while ($buffer = fgets($fd,4096))
		{
			$buffer = preg_replace("/\n/","",$buffer);   //chomp()
			$pieces = explode(",",$buffer);
			$countryCode = $pieces[0]; $countryName = $pieces[1];
			$countryArray[$countryCode] = $countryName;
		}
		fclose($fd);
		return $countryArray[$code];
	}


	cfunction getctrybyhost($hostname) {

		return($this->getctrybycode($this->getctrycodebyhost($hostname)));
	}

	cfunction getctrycodebyhost($hostname) {
		return(substr(strrchr($hostname,'.'),1));
	}

	cfunction MaskOtherIP($IP) {

		if($IP==getenv("REMOTE_ADDR"))
			    return($IP);

				 $IP=strtr($IP,"0123456789","##########");
				 return($IP);
	}

	cfunction getClientIP() {
		$IP = getenv('REMOTE_ADDR');
		return $IP;
	}

	cfunction getClientHostname()
	{
		$error = 0;
		$IP = $this->getClientIP();
		$hostname = gethostbyaddr($IP);

	   if(!strcmp($hostname,$IP)) $error = 1;		// if failure, gethostbyaddr() returns the IP
		if (!$error) //if no error
		{
			return $hostname;
		}			
		//else
		return "";
	}

	cfunction getClientCountry()
	{
		$error = 0;
		$hostname = $this->getClientHostname();
		if (!strcmp($hostname,"")) $error = 1;
		if (!$error)
		{
			$country = $this->getctrybyhost($hostname);
			return $country;
		}
		//else
      return "";
	}

	cfunction getClientFlag($size)
	{
		$error = 0;
		$hostname = $this->getClientHostname();
		if (!strcmp($hostname,"")) $error = 1;
		if (!$error)
		{
			$country_code = strtolower($this->getctrycodebyhost($hostname));
			$file_name = $this->flag_dirs[$size] . '/' . $country_code . '.' . $this->flag_ext;
			if (is_readable($file_name))
			{
				return $file_name;
			}
		}
		//else
      return "";
	}

	cfunction getClientFlagHTML($size)
	{
		$error = 0;
		$flag = $this->getClientFlag($size);
		if (!strcmp($flag,"")) $error = 1;
		if (!$error)
		{
			return '<img src="' . $flag . '">';
		}
		//else
      return "";
	}
};

Try the following:

<?php

define('SMALL', 0);
define('BIG',   1);

class ClientInfo {

	var $flag_dirs = array(SMALL => 'assets/flags/small', BIG => 'assets/flags/big'); 
	var $flag_ext  = 'png'; 

	function getctrybycode($code) {
		$countryArray = array();
		$input = "includes/countries.dat";
		$fd = fopen($input,"r") or die("Error: cannot open $input!");
		while ($buffer = fgets($fd,4096))
		{
			$buffer = preg_replace("/\n/","",$buffer);   //chomp()
			$pieces = explode(",",$buffer);
			$countryCode = $pieces[0]; $countryName = $pieces[1];
			$countryArray[$countryCode] = $countryName;
		}
		fclose($fd);
		return $countryArray[$code];
	}


	function getctrybyhost($hostname) {

		return($this->getctrybycode($this->getctrycodebyhost($hostname)));
	}

	function getctrycodebyhost($hostname) {
		return(substr(strrchr($hostname,'.'),1));
	}

	function MaskOtherIP($IP) {

		if($IP==getenv("REMOTE_ADDR"))
			    return($IP);

				 $IP=strtr($IP,"0123456789","##########");
				 return($IP);
	}

	function getClientIP() {
		$IP = getenv('REMOTE_ADDR');
		return $IP;
	}

	function getClientHostname()
	{
		$error = 0;
		$IP = $this->getClientIP();
		$hostname = gethostbyaddr($IP);

	   if(!strcmp($hostname,$IP)) $error = 1;		// if failure, gethostbyaddr() returns the IP
		if (!$error) //if no error
		{
			return $hostname;
		}			
		//else
		return "";
	}

	function getClientCountry()
	{
		$error = 0;
		$hostname = $this->getClientHostname();
		if (!strcmp($hostname,"")) $error = 1;
		if (!$error)
		{
			$country = $this->getctrybyhost($hostname);
			return $country;
		}
		//else
      return "";
	}

	function getClientFlag($size)
	{
		$error = 0;
		$hostname = $this->getClientHostname();
		if (!strcmp($hostname,"")) $error = 1;
		if (!$error)
		{
			$country_code = strtolower($this->getctrycodebyhost($hostname));
			$file_name = $this->flag_dirs[$size] . '/' . $country_code . '.' . $this->flag_ext;
			if (is_readable($file_name))
			{
				return $file_name;
			}
		}
		//else
      return "";
	}

	function getClientFlagHTML($size)
	{
		$error = 0;
		$flag = $this->getClientFlag($size);
		if (!strcmp($flag,"")) $error = 1;
		if (!$error)
		{
			return '<img src="' . $flag . '">';
		}
		//else
      return "";
	}
};
commented: Thanks! +0

actually, your code wasn't written good :)
the code expect function (a php... function) but instead it sees cfunction, which it sees as a string.

Or in one word: typo :P

@cwarn23

Thanks a bunch! That did help!

@phoenix_2000
You are right! :) Sometimes it's better to sleep instead of coding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.