Welcome to WebmasterWorld Guest from 107.21.163.40

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Free Geolocation script

Free Geolocation script that would detect visitor's country

     
7:16 pm on Dec 30, 2010 (gmt 0)

New User

5+ Year Member

joined:Sept 2, 2010
posts:32
votes: 0


Hi,

I'm looking for a free Geolocation script that would detect from which country is the visitor coming from. I need just the country detection, not town, provider, etc..

What I want to do is display one version of a webpage to users from country X and another one for country Y. Something as simple as:

if($country == "FR"): DISPLAY PAGE 1
if($country == "UK"): DISPLAY PAGE 2


(I have googled this, but found many options, so I'd like to ask you for a simple, fast and tested solution)

Thanks..
12:10 am on Dec 31, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member topr8 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 19, 2002
posts:3211
votes: 13


you need a database that matches ip's to countries, there are free ones available, you then have to keep it up to date, as it changes.

you then compare the user ip to the database and extract the country - pretty simple.

i'm not sure of any ready made scripts that do this, i imagine a quick search on your favourite search engine for something like:

free country to ip database

would return some results
5:25 am on Dec 31, 2010 (gmt 0)

Full Member

5+ Year Member

joined:Sept 8, 2010
posts: 210
votes: 0


With this i can help ! lol

Click Here to Download the DataBase:
[ip2nation.com...]
Upload it to phpmyadmin or whatever


Create a .PHP File with this Script :


$server = ''; // MySQL hostname
$username = ''; // MySQL username
$password = ''; // MySQL password
$dbname = ''; // MySQL db name


$db = mysql_connect($server, $username, $password) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());

$sql = 'SELECT
code
FROM
ip2nationCountries c,
ip2nation i
WHERE
i.ip < INET_ATON("'.$_SERVER['REMOTE_ADDR'].'")
AND
c.code = i.country
ORDER BY
i.ip DESC
LIMIT 0,1';

list($countryName) = mysql_fetch_row(mysql_query($sql));

// Output Country Code
echo $countryName;

?>


And thats it

if you want the country code to have uppercase letters replace:

SELECT CODE With SELECT iso_code_2

And dont forget that the database has country codes ,country , country latitude and longitude .....
4:52 pm on Dec 31, 2010 (gmt 0)

New User

5+ Year Member

joined:Sept 2, 2010
posts:32
votes: 0


Killer, thanks a lot for the script! It is working..

Only problem is that some IPs show as 'EU', but after I checked that IP on ip2location it shows as German. Is there any way to find out the country from these 'EU' IPs?
6:18 pm on Dec 31, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


geoplugin.com [geoplugin.com].
11:47 am on Jan 2, 2011 (gmt 0)

Full Member

5+ Year Member

joined:Sept 8, 2010
posts: 210
votes: 0


Hi
Apologizes For my late reply..

hmm... it seems that Countrys on Europe Territory got a EU CC :|
i dont use that Database anymore so i forgot what it had in xD

Chose one of this Databases ... [ipinfodb.com...]
when you decided on one tell me and i'll give you a code to query ..
i use the one with 3.5M IP records (3nd from top)

Or more easily use their API To Get the Country Code etc ..
[ipinfodb.com...]
4:36 pm on Jan 4, 2011 (gmt 0)

New User

5+ Year Member

joined:Sept 2, 2010
posts:32
votes: 0


Hi Killer,

I have downloaded and imported the 'Complete (Country)' database (the 5th from the top).
Could you please paste the query code?

Thanks..
5:22 pm on Jan 4, 2011 (gmt 0)

Full Member

5+ Year Member

joined:Sept 8, 2010
posts: 210
votes: 0




<?php

$server = ''; // MySQL hostname
$username = 'root'; // MySQL username
$password = ''; // MySQL password
$dbname = 'IPToCountry1'; // MySQL db name


$db = mysql_connect($server, $username, $password) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());

$sql = 'SELECT
country_code
FROM
ip_group_country
WHERE
ip_start <=INET_ATON("'.$_SERVER['REMOTE_ADDR'].'")
ORDER BY
ip_start DESC
LIMIT 0,1';
list($CountryCode) = mysql_fetch_row(mysql_query($sql));

?>

<?
echo $CountryCode;
?>


6:10 pm on Jan 4, 2011 (gmt 0)

New User

5+ Year Member

joined:Sept 2, 2010
posts:32
votes: 0


Thanks a lot!
9:09 pm on Jan 4, 2011 (gmt 0)

Full Member

5+ Year Member

joined:Sept 8, 2010
posts: 210
votes: 0


No Problem !

If you have any other Questions about GeoLocation let me know :)
but i rarely check for replys so it may take a while
11:31 pm on Jan 15, 2011 (gmt 0)

New User

5+ Year Member

joined:Jan 15, 2011
posts:1
votes: 0


@TheKiller

Thanks dude, i got it working. i know jack #*$! about php so im now a bit stuck.

the code works, when i vist the test.php file i get the result "uk" - my question is, how do i create a file that shows page1.php to anyone on a usa ip and then page2.php for the rest of the world.

i appreciate if you or anyone could help me in this matter.

thank in advance
4:25 pm on Jan 16, 2011 (gmt 0)

Full Member

5+ Year Member

joined:Sept 8, 2010
posts:210
votes: 0


hmm... i am wondering a few things ..
English is Spoken in a few countrys as main language ..
the only ones im aware of is USA ,UK(GB),CA(Canada)

it would be a bit complicated

i am not great at PHP Myself .. but here is a script i modified a bit


<?php
$CountryCode = ((isset($_GET['Lang']) && !empty($_GET['Lang'])) ? strip_tags($_GET['Lang']) :'US');

if($CountryCode == 'US'){
include("UnitedStates.html");
}

elseif($CountryCode == 'UK'){
include("UnitedKingdom.html");
}

else{
include("RestOfTheWorld.php");
}
?>


The main page Will be set to US if the visitor is from there..
if hes from UK he will be send to the UK Page
if the visitor is NOT From UK or US He will get to some "Error page"
i think it would be cool if you would make a Window to Poop-up
and ask them to select/click their language

Should Work... :-?