Location: PHPKode > projects > IP2Location™ IP-Country Database > ip2location-ip-country-database/PHP/Access/ipcountry.php
<HTML>
<HEAD><TITLE>
IP2Location IP-COUNTRY Database Demo in PHP Hypertext Preprocessor
</TITLE>
	<STYLE> 
		TABLE,BODY,P{ color:Black; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:12px; }
		H1{ color:Black; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:16px; }
		A.blue,A.blue:VISITED,A.blue:LINK{ text-decoration:none;font-weight:bolder; color:Blue; }
	</STYLE>
</HEAD>
<BODY bgcolor="#FFFFFF">
<center>
<h1><b><u>IP2Location?Demo - 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+
// Installation : a. Create a web directory in IIS.
//                b. Copy ipcountry.php and ../../IP-COUNTRY.SAMPLE.MDB into web directory.
//                c. Browse ipcountry.php using http protocol.
//                   example: http://localhost/ip2location/ipcountry.php.
//                d. Enter an IP address range between 202.181.128.0 and 203.99.71.255 and click submit.
//
// Author       : IP2Location.com
// URL          : http://www.ip2location.com
//
// Copyright (c) 2002-2008 IP2Location.com
//---------------------------------------------------------------------------


	echo	"  <form action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"POST\">\n";
	echo	"    <textarea name=\"ipaddress\" cols=15 rows=6></textarea><br>\n";
	echo	"    <input type=\"submit\" name=\"submit\" value=\"submit\">\n";
	echo	"  </form>\n";
	
	if (isset($_POST["ipaddress"])) 
	{
		// get the IP address from the form 
		$ipaddress = $_POST["ipaddress"];
		$ipaddress = str_replace(" ","\n", $ipaddress);
		$arripaddress = split("\n", $ipaddress);
		if (count($arripaddress) > 0)
		{
			echo	"<p>";
			echo	"<h1><u>Lookup Result</u></h1>";
			// display header
			echo	"<table border = 1>";
			echo	"<tr>";
			echo	"<td align=center>IP Address</td>";
			echo	"<td align=center>Country Name (Short)</td>";
			echo	"<td align=center>Country Name (Long)</td>";
			echo	"</tr>";
			for ($i = 0; $i <= (count($arripaddress)-1); $i++)
			{
				if ($arripaddress[$i] != "")
				{
					$ipno = Dot2LongIP($arripaddress[$i]);
					// check if the IP address is supported in demo version
					if (($ipno < 3400892416) || ($ipno > 3412281343)) 
					{
						echo	"IP address " . $arripaddress[$i] . " is not supported in demo version.<br>\n";
					} 
					else 
					{
						// getcwd() required >= PHP4
						$accesspath = getcwd() . "\\IP-COUNTRY.SAMPLE.MDB";
						$conn = new COM("ADODB.Connection") or die("Cannot start ADO"); 
						// Microsoft Access connection string.
						$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . $accesspath);
						// query string to lookup the country by matching the range of IP address number and execute the query
						$rs = $conn->Execute("SELECT * FROM ipcountry WHERE " . $ipno . " BETWEEN ipFROM AND ipTO");
						// display results
						if (!$rs->EOF) 
						{ 
							echo "<tr>";
							echo "<td align=center>" . $arripaddress[$i] . "</td>";
							$fv = $rs->Fields("countrySHORT");
							echo "<td align=center>" . $fv->value . "</td>";
							$fv = $rs->Fields("countryLONG");
							echo "<td align=center>" . $fv->value . "</td>";
							echo "</tr>";
						} 
						else 
						{
							echo "<tr>";
							echo "<td align=center>" . $arripaddress[$i] . "</td>";
							echo "<td align=center>-</td>";
							echo "<td align=center>-</td>";
							echo "</tr>";
						}
	 					$rs->Close(); 
					}
				}
			}
			echo	"</table>";
			echo	"</p>";
		}
		else
		{
			echo "Please enter IP address.";
		}
	}
	// Convert dotted IP address into IP number in long
	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.ip2location.com"">Click here to subscribe full database</A>]
		<BR>
		<BR>
		Copyright (c) 2002-2008 by <a href="http://www.ip2location.com">IP2Location.com</a>
		<P>
		<a href="http://www.ip2location.com/"><img src="http://www.ip2location.com/images/ip2location468x60_0.gif" border=""0""></a>
		</P>
		</center>
	</BODY>
</HTML>
Return current item: IP2Location&trade; IP-Country Database