Getting visitors IP information with PHP and CURL

Getting visitors IP information with PHP and CURL

Getting visitors IP information with PHP and CURL

The PHP snippet below will enable your webpage to fetch geo information about the visitor to the website according to their IP adress. We’ll be using the http://www.ipaddresslocation.org service for this example, and with a little page scrapping we can fetch the correct location data.

There are many use cases for this simple feature:
You can use this geo information to keep track of your visitors, although I would then recommend the Google Analytics approach (Brief introduction to Google Analytics).
Being able to get the user location, you can also place region specific advertisements …
Or be creative, and do some awesomeness with it …

What about the accuracy of this service?
Note that this will never be 100% accurate. The information will be according to your ISP’s acces points, where the last available acces point of the ISP will determine the value of information.

<?php
    $ip = $_SERVER['REMOTE_ADDR'];
    $ip_data = get_ip_info($ip);
    echo('Hostname ### ' . $ip_data['host'] . '<br/>');
    echo('IP Country ### ' . $ip_data['country'] . '<br/>');
    echo('IP Country Code ### ' . $ip_data['country_code'] . '<br/>');
    echo('IP Continent ### ' . $ip_data['continent'] . '<br/>');
    echo('IP Region ### ' . $ip_data['region'] . '<br/>');
    echo('IP Latitude ### ' . $ip_data['latitude'] . '<br/>');
    echo('IP Longitude ### ' . $ip_data['longitude'] . '<br/>');
    echo('Organization ### ' . $ip_data['organization'] . '<br/>');
    echo('ISP Provider ### ' . $ip_data['isp']);

    function get_ip_info($ip = NULL)
    {
       if(empty($ip)) return false;
       $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, 'http://www.ipaddresslocation.org/ip-address-locator.php');
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt($ch, CURLOPT_POST, true);
       curl_setopt($ch, CURLOPT_POSTFIELDS, array('ip' => $ip));
       $data = curl_exec($ch);
       curl_close($ch);    
       preg_match_all('/<i>([a-z\s]+)\:<\/i>\s+<b>(.*)<\/b>/im', $data, $matches, PREG_SET_ORDER);
       if(count($matches) == 0) return false;    
       $return = array();
       $format_labels = array(
            'Hostname'          => 'host',
            'IP Country'        => 'country',
            'IP Country Code'   => 'country_code',
            'IP Continent'      => 'continent',
            'IP Region'         => 'region',
            'IP Latitude'       => 'latitude',
            'IP Longitude'      => 'longitude',
            'Organization'      => 'organization',
            'ISP Provider'      => 'isp'
       );
       foreach($matches as $info)
       {
          if(isset($info[2]) && !is_null($format_labels[$info[1]]))
          {
             $return[$format_labels[$info[1]]] = $info[2];
          }
       }
       return (count($return)) ? $return : false;
    }
?>

The above PHP snippet will ouput following information (note: the information below will be according to your personal IP adress)


About the Author

Daan is a Creative-Geek who loves learning and sharing new techniques! Follow him on Twitter to keep up to date with the Creative-Geeks blog and other subjects. Contact him on e-mail : info[at]creative-geeks.com.