I want to know how can i Find Ip address of visitor and country of the visitors usign php script? Can anyone give me an example for this problem? Thanks in advance for any help
you can get the ip with the following <?php if ( isset($_SERVER["REMOTE_ADDR"]) ) { $ip=$_SERVER["REMOTE_ADDR"] . ' '; } else if ( isset($_SERVER["HTTP_X_FORWARDED_FOR"]) ) { $ip=$_SERVER["HTTP_X_FORWARDED_FOR"] . ' '; } else if ( isset($_SERVER["HTTP_CLIENT_IP"]) ) { $ip=$_SERVER["HTTP_CLIENT_IP"] . ' '; } echo 'Clients IP Address: ' . $ip; ?> you then need a database for ip to country location to lookup your ip location to determine the country
Check this site to get the Country of the IP: http://www.hostip.info/use.html Example: http://api.hostip.info/country.php?ip=12.215.42.19
if (isset($_SERVER['HTTP_X_FORWARD_FOR'])) { $ip = $_SERVER['HTTP_X_FORWARD_FOR']; } else { $ip = $_SERVER['REMOTE_ADDR']; }
Here is the full code for you.You may need to register @ api.ipinfodb.com for free acces IPkey to location finder. $IPkey = "xxfda5a3063c18xxxxxxxxxxxxxxxxxx";//change with your IP key $time_reg = time(); $IP = $_SERVER['REMOTE_ADDR']; $ucountry = "UNKNOWN"; $ucity = "NA"; if ($IP != "") { $content = get_content("http://api.ipinfodb.com/v2/ip_query.php?key=$IPkey&ip=$IP"); //invalid link for demo purpose // Ex: http://api.ipinfodb.com/v2/ip_query.php?key=<your_api_key>&ip=74.125.45.100- Example link $country = explode("\n",$content); $ucountry = strip_tags($country[4]);// country is retrieved $ucity = strip_tags($country[6]);// city is retrieved } function get_content($url) { $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_HEADER, 0); ob_start(); curl_exec ($ch); curl_close ($ch); $string = ob_get_contents(); ob_end_clean(); return $string; } PHP:
There is code for a PHP solution from IP2Location.com, but you will need to download the geolocation database files. <?php require_once('ip2location.class.php'); $ip = new ip2location; $ip->open('./databases/IP-COUNTRY-SAMPLE.BIN'); $record = $ip->getAll('35.1.1.1'); echo 'IP Address: ' . $record->ipAddress; echo 'IP Number: ' . $record->ipNumber; echo 'Country Short: ' . $record->countryShort; echo 'Country Long: ' . $record->countryLong; echo 'Region: ' . $record->region; echo 'City: ' . $record->city; echo 'ISP/Organisation: ' . $record->isp; echo 'Latitude: ' . $record->latitude; echo 'Longitude: ' . $record->longitude; echo 'Domain: ' . $record->domain; echo 'ZIP Code: ' . $record->zipCode; echo 'Time Zone: ' . $record->timeZone; echo 'Net Speed: ' . $record->netSpeed; echo 'IDD Code: ' . $record->iddCode; echo 'Area Code: ' . $record->areaCode; echo 'Weather Station Code: ' . $record->weatherStationCode; echo 'Weather Station Name: ' . $record->areaCode; echo 'MCC: ' . $record->mcc; echo 'MNC: ' . $record->mnc; echo 'Mobile Brand: ' . $record->mobileBrand; ?>