Go Back   Webmaster Forum > Development > Programming > PHP
REMOVE the ads below !
Reply
 
LinkBack Thread Tools
  # 1 (permalink)
Old
Senior Geek
Posts: 559
Join Date: Nov 2007
iTrader: (0)
Location: Romania
Check user's country via database. - 11-28-2007

Function takes in IP and returns country's code (abbreviated) There is a separate function and download you can get Which has the list of country codes and country names. Goto: PHPtricks.com/PHPskills.com to download it

PHP Code:
<?php 

/* 
    Function takes in IP and returns country's code (abbreviated) 
    There is a separate function and download you can get 
    Which has the list of country codes and country names 
    Goto: PHPtricks.com/PHPskills.com to download it 
*/ 


function check_country($ip) {     
    
$numbers preg_split"/\./"$ip);     
    include(
"my_ip_list/".$numbers[0].".php"); 
    
$code=($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]);     
    foreach(
$ranges as $key => $value){ 
        if(
$key<=$code){ 
            if(
$ranges[$key][0]>=$code){$country=$ranges[$key][1];break;} 
        } 
    } 
    if (
$country==""){$country="unkown";} 
    return 
$country


//Sample implementation 
$user_country check_country($_SERVER['REMOTE_ADDR']); 
echo 
"You are from: $user_country"

?>

Last edited by Shadab : 11-29-2007 at 05:44 AM.
Reply With Quote
  # 2 (permalink)
Old
A Lazy Freelance Web Developer
Posts: 2,121
Join Date: Feb 2007
iTrader: (0)
Location: Bhopal (MP, India)
Re: Check user's country via database. - 11-29-2007

Added the [php] [/php] tags around your actual code to make it look better.

Btw, thanks for sharing the script.
Simple yet useful.



Thanx,
Shadab.
Reply With Quote
  # 3 (permalink)
Old
Senior Geek
Posts: 559
Join Date: Nov 2007
iTrader: (0)
Location: Romania
Re: Check user's country via database. - 11-29-2007

OK Dude !
I will post more `tutorials` ..
Reply With Quote
Reply


Thread Tools



PSD to HTML

vBulletin®, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd. | SEO by vBSEO | Skin developed by vBStyles.com