Contact Info

Crumbtrail

ActiveXperts.com » Network Component » How to Use Network Component » IP to Country » PHP

PHP IPtoCountry Sample Source Code

Network Component provides an easy-to-use development interface to a variety of IP protocols. By using Network Component, you can very easily create or enhance applications with network features.

Network Component features the following: DNS, FTP, HTTP, HTTPs, ICMP Ping, IP-to-Country, MSN, NTP, RSH, SCP, SFTP, SNMP v1/v2c (Get, GetNext, Set), SNMP Traps, SNMP MIB, SSH, TCP, Telnet, TFTP, UDP, Telnet, Wake-On-LAN and more.

Network Component can be well integrated into any development platform that supports ActiveX objects.



Step 1: Download and install Network Component

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

Step 2: Create a form

To connect to an ftp server using PHP we need to collect the login information from the user, then connect to the server and then view the content

First of all we need to collect some information from the user. Like the username, the password and of course the server we are going to connect to. Perhaps you'd like to change the directory or/and create a logfile at logon. You can make the form look like this.

(Click on the picture to enlarge)

Step 3: Create the Network Component object in PHP

To create the Network Component object in php use the following code:

<? //create the Network Component IpToCountry object
$objHttp = new COM (AxNetwork.IpToCountry);
?>

Appendix: Full source code

<?php

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_ReplaceEx("http://", "", $strUrl);
     }
     else{
       return "error";
     }
  }



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

  function getCountry($host, $logfile){	
    $objSocket         = new COM('AxNetwork.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>ActiveXperts Network component 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>ActiveXperts Network component 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>
 <tr>
  <td>Hostname:</td>
  <td>
   <?php
     echo $url;
   ?>
  </td>
 </tr>
 <tr>
  <td>Country:</td>
  <td>
   <?php
     echo $arrHostnameLocation[0];
     echo " : ";
     echo $arrHostnameLocation[1];		 
   ?>
  </td> </tr>
 <tr>
  <td>Results:</td>
  <td>
   <?php
    echo $arrHostnameLocation[2];
    echo " : ";
    echo $arrHostnameLocation[3];
   ?>	
  </td>
 </tr>
</table>

</div>

</body>
</html>

You can download the complete samples here. There are many other working Network Component scripts on our site and shipped with the product.