I have already posted a script to find out the visitor’s ip. This one is similar to that one but has some additional functionality. This script uses the free IP Lookup API(Application Program Interface) provided by hostip . The script fetches the visitor’s IP from $_SERVER['REMOTE_ADDR'] and then passes on the ip to the HostIP.info API. Finally, the script processes the data returned by HostIp’s API.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<title>IP Lookup - MyCoding.net</title> <?php /* PHP IP Lookup script Author: Vlad Website: www.mycoding.net */ function getGeo(){ $ip = $_SERVER['REMOTE_ADDR']; $info = file_get_contents("http://api.hostip.info/get_html.php?ip=$ip"); $country = explode("\n",$info); $country[0] = str_replace("Country:","",$country[0]); return $country[1].",".$country[0]; } echo "Your IP: ".$_SERVER['REMOTE_ADDR']."<br>".getGeo(); ?> |
Demo: IP Lookup – MyCoding.net
Vlad
Latest posts by Vlad (see all)
- Code jam “Tic-Tac-Toe-Tomek” solution in java - April 19, 2013
- Code jam “Minimum Scalar product” solution in java - March 18, 2013
- Code jam Store Credit solution in java - March 10, 2013