homepage Welcome to WebmasterWorld Guest from 54.167.179.48
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Free Geolocation script
Free Geolocation script that would detect visitor's country
Jkjk



 
Msg#: 4247606 posted 7:16 pm on Dec 30, 2010 (gmt 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..

 

topr8

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



 
Msg#: 4247606 posted 12:10 am on Dec 31, 2010 (gmt 0)

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

TheKiller



 
Msg#: 4247606 posted 5:25 am on Dec 31, 2010 (gmt 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 .....

Jkjk



 
Msg#: 4247606 posted 4:52 pm on Dec 31, 2010 (gmt 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?

rocknbil

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



 
Msg#: 4247606 posted 6:18 pm on Dec 31, 2010 (gmt 0)

geoplugin.com [geoplugin.com].

TheKiller



 
Msg#: 4247606 posted 11:47 am on Jan 2, 2011 (gmt 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...]

Jkjk



 
Msg#: 4247606 posted 4:36 pm on Jan 4, 2011 (gmt 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..

TheKiller



 
Msg#: 4247606 posted 5:22 pm on Jan 4, 2011 (gmt 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;
?>



Jkjk



 
Msg#: 4247606 posted 6:10 pm on Jan 4, 2011 (gmt 0)

Thanks a lot!

TheKiller



 
Msg#: 4247606 posted 9:09 pm on Jan 4, 2011 (gmt 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

sirtokealot



 
Msg#: 4247606 posted 11:31 pm on Jan 15, 2011 (gmt 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

TheKiller



 
Msg#: 4247606 posted 4:25 pm on Jan 16, 2011 (gmt 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... :-?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved