Zend - The PHP Company




Miscellaneous

Add Code


Lookup Country by IP Address  

Type: code fragment
Added by: tgotchi
Entered: 21/10/2002
Last modified: 31/10/2001
Rating: - (fewer than 3 votes)
Views: 9981
This PHP script shows how to lookup country origin by using visitor's IP address. This example required an external ACCESS database for lookup purpose. Full database is available at http://www.location.com.my. Developers use this script for projects related to: 1. Display native language and currency 2. Redirect based on country 3. Digital rights management 4. Prevent password sharing and abuse of service 5. Reduce credit card fraud 6. Web log stats and analysis 7. Auto-selection of country on forms 8. Filter access from countries you do not do business with 9. Geo-targeting for increased sales and click-thrus 10. And much, much more!


<HTML>
<HEAD><TITLE>
IP - Country Database Demo in PHP Hypertext Preprocessor
</TITLE></HEAD>
<BODY bgcolor="#FFFFFF">
<h1><b><u>Lookup Country by IP Address</u></h1></b>
Enter a dotted IP address (xxx.xxx.xxx.xxx) between <b>202.181.128.0</b> and <b>203.99.71.255</b>.<br>
<?php
//---------------------------------------------------------------------------
// Title        : Lookup Country by IP Address
// Description  : This script lookup the country by IP address.
//                Things that we can do with this script.
//                        1. Display native language and currency 
//                        2. Redirect based on country 
//                        3. Digital rights management 
//                        4. Prevent password sharing and abuse of service 
//                        5. Reduce credit card fraud 
//                        6. Web log stats and analysis 
//                        7. Auto-selection of country on forms 
//                        8. Filter access from countries you do not do business with 
//                        9. Geo-targeting for increased sales and click-thrus 
//                        10. And much, much more! 
// Requirements : PHP 4+ and MS-ACCESS 2000+ COM
// Installation : a. Copy this php file (ipcountry.php) and  mdb database file (ipcountry.mdb)
//                   into a web directory.
//                b. Browse this webpage (ipcountry.php) using http protocol. Example, http://localhost/demo/ipcountry.php
//                c. Enter an IP address range between 202.181.128.0 and 203.99.71.255 and submit the form.
//
// Author       : Hexa Software Development Center
// URL          : http://www.location.com.my
// Copyright (c) 2002 by Hexa Software Development Center
//---------------------------------------------------------------------------


    
$ipaddress $HTTP_POST_VARS["ipaddress"];
    echo    
"  <form action="" . $_SERVER['PHP_SELF'] . "" method="POST">n";
    echo    
"    <input type="text" name="ipaddress">n";
    echo    
"    <input type="submit" name="submit" value="submit">n";
    echo    
"  </form>n";
    
    if (isset(
$HTTP_POST_VARS["ipaddress"])) {
        
$ipno Dot2LongIP($ipaddress);
        if ((
$ipno 3400892416) || ($ipno 3412281343)) {
            echo    
"IP address " $ipaddress " is not supported in demo version.<br>n";
        } else {
            
// getcwd() required >= PHP4
            
$accesspath getcwd() . "\IPCountry.mdb";
            
$conn = new COM("ADODB.Connection") or die("Cannot start ADO"); 
            
// Microsoft Access connection string.
            
$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" $accesspath);
            
// SQL statement to build recordset.
            
$rs $conn->Execute("SELECT * FROM IPCountry WHERE " $ipno " BETWEEN IPCountry.ipFROM AND IPCountry.ipTO");

                echo    
"<p>";
                echo    
"<h1><u>Lookup Result</u></h1>";
                echo    
"<table>";
                echo    
"<tr><td>IP Address :</td><td> <b>" $ipaddress "</b></td></tr>n";
            if (!
$rs->EOF) { 
                
$fv $rs->Fields("countrySHORT");
                echo 
"<tr><td>Country Name (Short) :</td><td> <b>" $fv->value "</b></td></tr>n";
                
$fv $rs->Fields("countryLONG");
                echo 
"<tr><td>Country Name (Long)  :</td><td> <b>" $fv->value "</b></td></tr>n";
            } else {

                echo 
"<tr><td>Country Name (Short) :</td><td> <b> - </b></td></tr>n";
                echo 
"<tr><td>Country Name (Long)  :</td><td> <b> - </b></td></tr>n";
                }
            
$rs->Close(); 
                echo    
"</table>";
                echo    
"</p>";
        }
    }

    function 
Dot2LongIP ($IPaddr)
    {
        if (
$IPaddr == "") {
            return 
0;
        } else {
            
$ips split (".""$IPaddr");
            return (
$ips[3] + $ips[2] * 256 $ips[1] * 256 256 $ips[0] * 256 256 256);
        }
    }
?>
<BR>
[<A HREF="http://www.location.com.my">Click here to purchase full database</A>]
<BR>
<BR>
Copyright (c) 2002 by Hexa Software Development Center
</BODY>
</HTML>


Usage Example


See the example


Rate This Script





Search



This Category All Categories