F4LLCON F4LLCON - 3 months ago 21
PHP Question

php geo ip loc does not work

http://chir.ag/projects/geoiploc/

Hello, I am trying to set this up, but whenever I use

include("geoiploc.php");
the page is blank and whenever I remove
include("geoiploc.php");
I only see my IP address. I've uploaded geoiploc.php and index.php to a webhost that can run PHP.

If there is ANY other easier way to show country name by IP or any other way, which is it? Please I need this fast

So as I said, it only shows my IP whenever I remove
include("geoiploc.php");
why?

You need this library: http://chir.ag/projects/geoiploc/autogen/geoiploc.tar.gz and this is the code to run the script:

include("geoiploc.php"); // Must include this

// ip must be of the form "192.168.1.100"
// you may load this from a database
$ip = $_SERVER["REMOTE_ADDR"];
echo "Your IP Address is: " . $ip . "<br />";

echo "Your Country is: ";
// returns country code by default
echo getCountryFromIP($ip);
echo "<br />\n";

// optionally, you can specify the return type
// type can be "code" (default), "abbr", "name"

echo "Your Country Code is: ";
echo getCountryFromIP($ip, "code");
echo "<br />\n";

// print country abbreviation - case insensitive
echo "Your Country Abbreviation is: ";
echo getCountryFromIP($ip, "AbBr");
echo "<br />\n";

// full name of country - spaces are trimmed
echo "Your Country Name is: ";
echo getCountryFromIP($ip, " NamE ");
echo "<br />\n";

Joe Joe
Answer

You can use the free MaxMind Geo Lite. Download the files here: http://www.maxmind.com/download/geoip/api/php/php-latest.tar.gz

Then download the Geo Country database from here: http://dev.maxmind.com/geoip/legacy/geolite

You can now use it like this:

<?php

    include("geoip.inc");

    function ipAddress(){
        if (!empty($_SERVER['HTTP_CLIENT_IP'])){ //check ip from share internet
            $ip=$_SERVER['HTTP_CLIENT_IP'];
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ // proxy pass ip
            $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
            $ip=$_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }

    $gi = geoip_open("path/to/GeoIP.dat",GEOIP_STANDARD);

    echo geoip_country_name_by_addr($gi, ipAddress());
    // echo geoip_country_code_by_addr($gi, ipAddress()); <-- country code   

    geoip_close($gi);

?>

UPDATE: to get the user's city you should download the top link and look for the file called sample_city.php to see some example code. You'll need to download this file: http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz and place it in the same directory as your php file. A quick example would be:

<?php

    include("geoipcity.inc");
    function ipAddress(){
        if (!empty($_SERVER['HTTP_CLIENT_IP'])){ //check ip from share internet
            $ip=$_SERVER['HTTP_CLIENT_IP'];
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ // proxy pass ip
            $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
            $ip=$_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }
    $gi = geoip_open("GeoIPCity.dat",GEOIP_STANDARD);
    $record = geoip_record_by_addr($gi,ipAddress());
    print $record->country_code . " " . $record->country_code3 . " " . $record->country_name . "\n";
    geoip_close($gi);

?>