ActiveSocket can be well integrated into PHP environments. This document describes how ActiveSocket can be integrated into PHP projects.
Download ActiveSocket from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.
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>
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");
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.