Contact Info

Crumbtrail

ActiveXperts.com » Network Component » How to Use Network Component » Telnet » PHP

PHP Telnet 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 Telnets object
$objTcp = new COM (AxNetwork.Tcp);
?>

Appendix: Full source code

<html>
<head>
<title>ActiveXperts Network component sample in PHP</title>
<style>
body{
   background-color: white;
	 font-family: verdana;
	 font-size: xx-small;
	 color: navy;
	 text-align: center;
}
input{
   font-family: verdana;
	 font-size: xx-small;
	 color: navy;
	 width: 300px;
	 border: 1px solid navy;
	 background-color: white;
}
table{
   border: 1px solid navy;
	 width: 370px;
}
td{
	 text-align: left;
   background-color: white;
	 font-family: verdana;
	 font-size: xx-small;
	 color: navy;
}
td.red{
	 text-align: left;
   background-color: white;
	 font-family: verdana;
	 font-size: xx-small;
	 color: red;
}
a{
	 text-align: left;
   background-color: white;
	 font-family: verdana;
	 font-size: xx-small;
	 color: red;
}
</style>
</head>
<body>
<?php
//==============================================================================
//======================== echo the form =======================================
//==============================================================================

function display_form(){
   //echo form
   echo "<form method=post action=" . $_SERVER['PHP_SELF'] . "?execute=ready>";
	 
   //echo the inputs in a table
   echo "<h2>ActiveXperts Network component PHP Sample (Telnet)</h2>";
   echo "<table border=0>";
   echo "<tr>";
   echo "   <td colspan=2 class=red>";
   echo "      This sample uses PHP and ActiveXperts Network component to create a Telnet-connection with another computer. Fill in the form and press the 'submit' button to set up the connection.<br><br>";
   echo "   </td>";
   echo "</tr>";	 
   echo "<tr>";
   echo "   <td>";
   echo "      Port:";
   echo "   </td>";
   echo "   <td>";
   echo "      <input type=text name=port value=23 ><br>";
   echo "   </td>";
   echo "</tr>";
   echo "<tr>";
   echo "   <td>";
   echo "      Command:";
   echo "   </td>";
   echo "   <td>";
   echo "      <input type=text name=command value=library >";
   echo "   </td>";
   echo "</tr>";
   echo "<tr>";
   echo "   <td>";
   echo "      Server:";
   echo "   </td>";
   echo "   <td>";
   echo "      <input type=text name=server value=telnet.activexperts-labs.com ><br>";
   echo "   </td>";
   echo "</tr>";
   echo "<tr>";
   echo "   <td>";
   echo "      Logfile:";
   echo "   </td>";
   echo "   <td>";
   echo "      <input type=text name=logfile value=C:\logfile.txt><br>";
   echo "   </td>";
   echo "</tr>";	 
   echo "<tr>";
   echo "   <td> </td>";
   echo "   <td height=30>";	 
   echo "      <input type=submit value=submit!>";
   echo "   </td>";
   echo "</tr>";
   echo "</table>";
   echo "<form>";
}

//==============================================================================
//========================== execute the command ===============================
//==============================================================================

function execute_command($port, $command, $server, $logfile){
   //create the object
   $objSocket = new com("AxNetwork.Tcp");
	 
   //set the logfile
   $objSocket->logfile = $logfile;
	 
   //connect the server
   $objSocket->Connect($server, $port);
   check_for_errors($objSocket->lasterror, $objSocket->getErrorDescription($objSocket->lasterror));

   //recieve the welcome message, we don't want to display that...
   $objSocket->ReceiveString();
   check_for_errors($objSocket->lasterror, $objSocket->getErrorDescription($objSocket->lasterror)); 
	 
   //now execute the command
   $objSocket->SendString($command);
   check_for_errors($objSocket->lasterror, $objSocket->getErrorDescription($objSocket->lasterror)); 

   //wait 1 second, so the command can be executed
   $objSocket->Sleep(5000);
	 
   //get the results
   echo "<h2>ActiveXperts Network component<br>sample with php</h2>";
   echo "<table border=0>";
   echo "<tr>";
   echo "   <td colspan=2 class=red>Results:</td>";
   echo "</tr>";
   echo "<tr>";
   echo "   <td colspan=2>";
   echo nl2br($objSocket->ReceiveString());
   echo "   <br><br></td>";
   echo "</tr>";
   echo "<tr>";
   echo "   <td colspan=2 class=red>Execute another command:</td>";
   echo "</tr>";
   echo "<tr>";
   echo "   <td valign=top>Command</td>";
   echo "   <td valign=top>";
   echo "			<form method=post action=" . $SERVER['PHP_SELF'] . "?execute=ready>";
   echo "         <input type=text name=command>";
   echo '         <input type=hidden name=server value="' . $server . '">';
   echo '         <input type=hidden name=port value="' . $port . '">';
   echo '         <input type=hidden name=logfile value="' . $logfile . '">';	 
   echo '         <input type=submit value="Submit!">';	 
   echo "     </form>";
   echo "   </td>";
   echo "</tr>";	 
   echo "</table>";
	 
   //check for errors
   check_for_errors($objSocket->LastError, $objSocket->GetErrorDescription($objSocket->LastError)); 
	 
   //close objSocket
   $objSocket->Disconnect;
	 
}

//==============================================================================
//========================== display the errors ================================
//==============================================================================

function check_for_errors($errorno, $errormessage){
   //if the errornumber isn't 0 (succes), we're going to display the errornumber and message
   if($errorno !=0){
      echo "<script>";
      echo "   alert('" . $errorno . ":" . $errormessage . "');";
			echo "   window.navigate('" . $_SERVER['PHP_SELF'] . "');";
      echo "</script>";
   }
}

//==============================================================================
//====================== display the right thing ==============================
//==============================================================================

//check if the form is submitted by the user
if($_GET['execute'] == "ready"){
   //if the form is submitted, execute the given commands
   execute_command($_POST['port'], $_POST['command'], $_POST['server'], $_POST['logfile']);
}
else{
   //otherwise, we're going to display the form
   display_form();
}

?>
this is an <a href=https://www.activexperts.com>activeXperts</a> sample
</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.