Reply
What PHP function would get me the country out of the IP address
Old 08-17-2009, 11:31 PM What PHP function would get me the country out of the IP address
Skilled Talker

Posts: 88
Trades: 0
I want to be able to obtain the nation/country of where the visitor is from.

Is there anything I can use to get such information, when all I have is the IP address?
TRANZIT JIM is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 08-18-2009, 08:21 AM Re: What PHP function would get me the country out of the IP address
Skilled Talker

Posts: 65
Trades: 0
I wanted to do the same with mysoftware ( WITHOUT USING 3rd PARTY WEBSITES OR DATABASES! This is done on the fly by your OWN server )

After a while, i came up with this:

Here goes,
1) Your php script needs access to run the a whois command.
2) Your server needs to be linux.

You will need to have this snippet on the page your visitors land on.
Code:
<?php
$ip = $_SERVER['REMOTE_ADDR']; // Get users IP

$country = exec("whois $ip  | grep -i country"); // Run a local whois and grep for the word country.
$country = strtolower($country); // lowercase everything.
// Get rid of unwanted whois response.
$country = str_replace("country:", "", "$country"); // Clean stuff unwanted from whoisis
$country = str_replace("network:country-code:", "", "$country"); // Clean stuff unwanted from whoisis
$country = str_replace("network:organization-usa", "", "$country"); // Clean stuff unwanted from whoisis
// End get rid of.
$country = str_replace(" ", "", "$country"); // get rid of any spaces.

echo $country; // this will now be a 2char country code, IE:  gb 
?>
You can then use flag icons from somewhere like famfamfam to display a flag icon based on the country code.

ie:
Code:
<?php

echo '<img alt="'.$country.'" src="Images/flags/png/'.$country.'.png" width="16" height="11" /> <b>('.$country.')</b>';
It works very well for me where i use it to map ips that have visited the site to a country and put a flag next to it.
It gets the country for an IP 90% to 95% of the time, Others will just come back as empty vars ( so work in a if var == "" then flag = unknown. )
Its more accurate that paid databases as whois is updated by the isp themselves.

Hope this helps anyone else out there.

This code can be changed to take any data from a whois. ( ie the ISP name, town location etc )

The whole point of this is so you wont need to rely on a 3rd party database or some other 3rd party website.
Whois is far more reliable.

Thanks
G
__________________
Talk to your visitors in realtime.
Live realtime support chat - www.IMsupporting.com

Last edited by lynxus; 08-18-2009 at 08:30 AM..
lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 08-18-2009, 08:26 AM Re: What PHP function would get me the country out of the IP address
chrishirst's Avatar
Super Moderator

Posts: 22,281
Location: Blackpool. UK
Trades: 0
From my bookmarks
http://www.daniweb.com/forums/thread77998.html
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Growing old is mandatory - Growing up is optional
Code Samples | People Counting System | Bits & Bobs
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-18-2009, 08:37 AM Re: What PHP function would get me the country out of the IP address
Skilled Talker

Posts: 88
Trades: 0
The server I use is owned by a web hosting company.

I am sure I had asked this question in here before but was not able to find it in a search.

All that I am seeking to do is, to return the two letter code so as to store in a database table.
TRANZIT JIM is offline
Reply With Quote
View Public Profile
 
Old 08-18-2009, 09:04 AM Re: What PHP function would get me the country out of the IP address
Skilled Talker

Posts: 65
Trades: 0
Then i think you will need to use a 3rd party site to generate the code.
Take a look at the bookmark chrishirst said.
__________________
Talk to your visitors in realtime.
Live realtime support chat - www.IMsupporting.com
lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 08-22-2009, 07:02 AM Re: What PHP function would get me the country out of the IP address
Skilled Talker

Posts: 88
Trades: 0
Ok, I got the following script

$country = file_get_contents('http://api.hostip.info/country.php?ip='.$IP);

I got the following error codes

Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in

Warning: file_get_contents(http://api.hostip.info/country.php?ip=) [function.file-get-contents]: failed to open stream: no suitable wrapper could be found in
TRANZIT JIM is offline
Reply With Quote
View Public Profile
 
Old 08-25-2009, 11:23 AM Re: What PHP function would get me the country out of the IP address
Skilled Talker

Posts: 65
Trades: 0
Looks like whoever you get your hosting from has disabled that function.

if you can, check your php.ini
__________________
Talk to your visitors in realtime.
Live realtime support chat - www.IMsupporting.com
lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 08-26-2009, 01:20 AM Re: What PHP function would get me the country out of the IP address
Average Talker

Posts: 17
Name: Jeremy
Location: WV
Trades: 0
Quote:
Originally Posted by TRANZIT JIM View Post
The server I use is owned by a web hosting company.

I am sure I had asked this question in here before but was not able to find it in a search.

All that I am seeking to do is, to return the two letter code so as to store in a database table.
Check MaxMind.com
__________________
Adscend Media, CPA Affiliate Network
Monetize your content with our content gateway! Demo
AdscendJeremy is offline
Reply With Quote
View Public Profile Visit AdscendJeremy's homepage!
 
Old 08-28-2009, 02:56 AM Re: What PHP function would get me the country out of the IP address
Super Talker

Posts: 120
Name: David Jackson
Trades: 0
yes

had deallings with maxmind and it seems like a good product
davidj is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to What PHP function would get me the country out of the IP address
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.20645 seconds with 13 queries