Looking for a Geolocation script

premiumsource

Senior Member
Joined
Apr 20, 2008
Messages
843
Reaction score
1,186
I am looking for a script that will display a geolocation on my site. It needs to be able to do something like Welcome visitor from (City). I am not looking for a script that pulls data from their ISP though. Any input would be appreciated. Thanks.
 
http://www.maxmind.com/app/geolitecity

They have a free good library with monthly updates. (I'd recommend using the binary version unless you have some major need to use a database.) It's not as accurate as their paid version, but it's not half bad. (99.5% accuracy country-wise vs 99.8% and 79% vs 83% city-wise.) Their site also includes different template code for C, C#, PHP, Java, Pearl, etc.

Example code links:
http://www.maxmind.com/app/support
 
I have my own PHP script, which does that. PM if interested
 
Thanks for your PM trudat, but I just realize I can't respond to PMs while I don't have more than 15 posts. So I will share it here.

Guys, firstly download geoip script here:

mediafirecom/?fw89d42geuvskhb

Upload it on your server

Once you open it, you'll see something like this:

Code:
<?php
    // the function returns the ip address of the person viewing the page
    function userIP()
    {
        return (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']);
    }
    
    //    here we include the necessary files for the geoip script
    include("./geoip/geoip.inc");
    include("./geoip/geoipcity.inc");
    include("./geoip/geoipregionvars.php");
    
    // we open the database file     
    $gi = geoip_open("./geoip/GeoLiteCity.dat", GEOIP_STANDARD);
    
    // we optain the necessary datails about the IP
    $rsGeoData = geoip_record_by_addr($gi, userIP());
    
    //close the database file
    geoip_close($gi);
        
    $city = ($rsGeoData->city);        // $city stores the name of the city coresponding to the IP 
    $randNr = rand(1000,2000);        // generate a random number between 1000 and 2000
    
    // because the accuracy of the data base is not 100% for every country
    // we show an alternative text if the city could not be found    
    if($city == ""){
        echo "[B]We`ve Found $randNr Black Singles in Your Area Online Now![/B]";
    }
    else{
        // show the message
        echo "[B]We`ve Found $randNr Black Singles in $city Online Now![/B]";
    }                

?>
Bold text is what you need to edit.

Lets say you have line "Welcome visitor from City on my website" ..you will need to place this text to:

Code:
if($city == ""){
        echo "[B]Welcome visitor from City on my website[/B]";
    }
    else{
        // show the message
        echo "[B]Welcome visitor from [U]$City[/U] on my website[/B]";
    }
If it's not clear, let me know and I will help you to install it.
 
Last edited:
HTTP_X_FORWARDED_FOR - this is very frequently mistake of many programmers.

Don't use it, becose this is http header and client can send anything in this header.

PHP:
function userIP()
    {
        return (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']);
    }


to


PHP:
function userIP()
    {
        return  $_SERVER['REMOTE_ADDR'];
    }
 
HTML5 supports Geolocation now, so use that for all the main browsers, and then code something with maxmind or another geoip service if you detect the browser doesn't support HTML5.
 
Back
Top