IP to Country component for PHP for Windows

ActiveSocket is a Network Communication component for Windows Developers. It runs on any 32 bit and 64 bit Windows Platform, incl. Windows 7, Windows 2008, Windows 2003, Windows 2000, Windows Vista and Windows XP.
It features many IP protocols, incl.: SSH (Secure Shell), RSH (Remote Shell), HTTP(s), FTP, ICMP Ping, NTP, SNMP v1/v2c (Get,GetNext,Set), SNMP MIB translation, SNMP Trap Sender, SNMP Trap Receiver, Telnet, DNS, TCP, UDP, IP-to-Country, Wake-On-LAN and more. Samples are included many popular development platforms, incl. Visual C# .NET, Visual Basic .NET, ASP .NET, Visual Basic, Visual C/C++, ASP, Java, Javascript, PHP, Borland Delphi, Borland C++ Builder, ColdFusion.

ActiveSocket can be well integrated into PHP environments. This document describes how ActiveSocket can be integrated into PHP projects.

Step 1: Download and install ActiveSocket

Download ActiveSocket from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.

Step 2: Create a form

First of all we have to create a form in PHP to enter the IP address or Host name, execute the query and display the results.
The fields are named: strHostname, strLogfile, submitButton. The full code of this form is displayed below:

<form method=get>

<h2>ActiveSocket IP to Country PHP Sample</h2>

<font face=verdana color=black size=1><b>Please fill in the form:</b></font>

<table>
 <tr>
  <td>The hostname you want to know the location of:</td>
  <td><input size=50 name=strHostname value="forum.activexperts.com"></td>
 </tr>
 <tr>
  <td>The file you want to write a log to:</td>
  <td><input size=50 name=strLogfile value="C:\logfile.txt"></td>
 </tr>
 <tr>
  <td></td>
  <td><input type=submit name=submitbutton value="Convert hostname to country"></td>
 </tr>
</table>
</form>

Step 3: Create the a function that executes the commands

Once we have the information from the user, we need to execute it. First we have to create an instance of the ActiveSocket object. Use the following PHP code to create the object:

    $objSocket = new COM ("ActiveXperts.Tcp");

Step 4: Performing an IP to Address conversion

After an instance of the object is created we can create the PHP code to perform an IP to Country lookup. You can find the code of a simple online IP to Country lookup utility below:

<?

if( $_GET['submitbutton'] != "" ){

  //###### this function checks the hostname ##############################
  //###### the hostname cannot have "http://" in it, so strip that ########

  function checkUrl($strUrl){
     if($strUrl != ""){
       return str_replace("http://", "", $strUrl);
     }
     else{
       return "error";
     }
  }



  //###### this function resolves the hostname its location ###############

  function getCountry($host, $logfile){	
    $objSocket         = new COM('ActiveXperts.IPtoCountry');
    if($host != "error"){
      $objSocket->Logfile = $logfile;                                           //Fill in the logfile position
      $objSocket->Host = $host;                                                 //Fill in the hostname you want to resolve the location from
      $objSocket->Query;                                                        //Execute a query
      $result[0] = $objSocket->CountryCode;                                     //Return the countrycode, something like "NL", "UK"
      $result[1] = $objSocket->CountryName;                                     //Return the full country name
      $result[2] = $objSocket->LastError;                                       //Return the results
      $result[3] = $objSocket->GetErrorDescription($objSocket->LastError);      //Return a description of the results
      return $result;                                                           //return the results in an array
      $objSocket = null;                                                        //Empty $objSocket
    }
  }

  $logfile   = $_GET['strLogfile'];
  $url       = $_GET['strHostname'];
  $url       = checkUrl($url);                                                  //Check $url
  $arrHostnameLocation = getCountry($url, $logfile);                            //Fetch the countryID, countryname, lasterror and errordescription
}


?>

<html>
<head>
  <title>ActiveSocket IP To Country Sample</title>
  <style type="text/css">
   <!--
   table{
     border: 1px solid navy;
     font-family: verdana;
     font-size: x-small;
     color: navy;
   }
   input{
     border: 1px solid navy;
     background-color: white;
     font-family: verdana;
     font-size: x-small;
     color: navy;
   }
   h2{
     font-family: verdana;
     color: navy;
   }
   -->
  </style>
</head>

<body>

<div align=center>

<form method=get>

<h2>ActiveSocket IP to Country Sample<br>In PHP</h2>

<font face=verdana color=black size=1><b>Please fill in the form:</b></font>

<table>
 <tr>
  <td>The hostname you want to know the location of:</td>
  <td><input size=50 name=strHostname value="http://forum.activexperts.com"></td>
 </tr>
 <tr>
  <td>The file you want to write a log to:</td>
  <td><input size=50 name=strLogfile value="C:\logfile.txt"></td>
 </tr>
 <tr>
  <td></td>
  <td><input type=submit name=submitbutton value="Convert hostname to country"></td>
 </tr>
</table>
</form>
<font face=verdana color=black size=1><b>The results:</b></font>
<table width=705>
 <tr>
  <td width=150>Hostname:</td>
  <td>
   <?
     echo $url;
   ?>
  </td>
 </tr>
 <tr>
  <td width=150>Country:</td>
  <td>
   <?
     echo $arrHostnameLocation[0];
     echo " : ";
     echo $arrHostnameLocation[1];		 
   ?>
  </td> </tr>
 <tr>
  <td width=150>Results:</td>
  <td>
   <?
    echo $arrHostnameLocation[2];
    echo " : ";
    echo $arrHostnameLocation[3];
   ?>	
  </td>
 </tr>
</table>
</div>
</body>
</html>

There are many working samples included with the product. You can also find them on the ActiveXperts FTP site: ftp.activexperts-labs.com/samples/asocket.