How to find ip address and country using php

Discussion in 'PHP' started by bruce_clay, Feb 16, 2011.

  1. #1
    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
     
    bruce_clay, Feb 16, 2011 IP
  2. MikeBuyco

    MikeBuyco Member

    Messages:
    702
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    35
    #2
    Very good question bruce. I want to learn this one also.
     
    MikeBuyco, Feb 16, 2011 IP
  3. srisen2

    srisen2 Peon

    Messages:
    359
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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
     
    srisen2, Feb 16, 2011 IP
  4. rendom

    rendom Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    To get location as country and state you will need GEOIP database. maxmind.com
     
    rendom, Feb 17, 2011 IP
  5. domainwink

    domainwink Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    domainwink, Feb 17, 2011 IP
  6. chainas

    chainas Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    if (isset($_SERVER['HTTP_X_FORWARD_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARD_FOR'];
    } else {
    $ip = $_SERVER['REMOTE_ADDR'];
    }
     
    chainas, Feb 17, 2011 IP
  7. hilhilginger

    hilhilginger Well-Known Member

    Messages:
    324
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #7
    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:
     
    Last edited: Feb 18, 2011
    hilhilginger, Feb 18, 2011 IP
  8. hexahow

    hexahow Greenhorn

    Messages:
    10
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    13
    #8
    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;

    ?>
     
    hexahow, Feb 22, 2011 IP
    Vooler likes this.