Location: PHPKode > scripts > IP Sniffing > class.ipsniffing.php
<?php
/**
 * @uses The function is used for IP Sniffing 
 * 		 (i.e. to track the user location from the IP of the system used).
 * 
 * @pattern Singleton
 * 
 * @author Girish Madhavan Nambiar
 * @since Wednesday, June 10, 2009
 */
class IPSniffing {
	/**
	 * @var remoteAddress
	 * @type string
	 */
	var $remoteAddress = '';

	/**
	 * @var objResult
	 * @type object SimpleXMLElement
	 */
	var $objResult = '';

	/**
	 * @var ipSniffed
	 * @type boolean
	 * @scope static
	 * @default false
	 */
	static $ipSniffed	=	false;

	/**
	 * @var countryName
	 * @type string
	 */
	var $countryName = '';

	/**
	 * @var countryCode
	 * @type string
	 */
	var $countryCode = '';

	/**
	 * @var regionCode
	 * @type string
	 */
	var $regionCode = '';

	/**
	 * @var cityName
	 * @type string
	 */
	var $cityName = '';

	/**
	 * @uses Constructor to the class IPSniffing
	 */
	function __construct() {
		$this->remoteAddress	=	$_SERVER['REMOTE_ADDR'];//,'123.238.71.125'
	}// invalid arbitrary IP '272.232.196.231'
	
	/**
	 * @uses use CURL for calling the API functionality.
	 * @param string url for calling the API functionality.
	 * @return void
	 */
	function sendURL($strAPIURL) {
		// Initialisation  
		$ch=curl_init();  
		
		// Set parameters  
		curl_setopt($ch, CURLOPT_URL, $strAPIURL);
		curl_setopt($ch, CURLOPT_VERBOSE, 1);
		
		// Return a variable instead of posting it directly  
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
		
		// Active the POST method  
		curl_setopt($ch, CURLOPT_POST, 1) ;  
		
		// Request  
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);  
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);  
		// execute the connexion  
		$xmlResult = curl_exec($ch);  
		
		// Close it  
		curl_close($ch);  
		
		// no response
		if( ($xmlResult == '') || (strlen($xmlResult) < 2)) {
			die("CURL execution failed. Kindly check your firewall permissions");
		}
		
		$this->objResult = simplexml_load_string($xmlResult);

	}

	/**
	 * @uses main function in this class for IP Sniffing.
	 * @param void
	 * @return void
	 */
	function sniffIP() {
		if($this->ipSniffed	== false) {
			$strAPIURL = "http://ipinfodb.com/ip_query.php?ip=".strval($this->remoteAddress);
			
			// clean the url for API call.
			$strAPIURL = str_replace(" ", "%20", $strAPIURL);
			$strAPIURL = str_replace("-", "%20", $strAPIURL);
			
			// make a call to api an fetch result.
			$this->sendURL($strAPIURL);
			
			// check for data and set the flag if ok. //, 'SimpleXMLElement'
			if( is_object($this->objResult) && (strval($this->objResult->CountryName) != '') && (strval($this->objResult->City) != '') ) {
				$this->countryName	=	strval($this->objResult->CountryName);
				$this->countryCode	=	strval($this->objResult->CountryCode);
				$this->cityName		=	strval($this->objResult->City);

				$this->ipSniffed	=	true;
				#$_SESSION['ipSniffed']	=	"yes";
			}
		}
	}// End of function.

}// End of class.
?>
Return current item: IP Sniffing