This article mainly introduces the PHP implementation of the Telnet function example, the need for friends can refer to the following
Code as follows: <?php class Phptelnet { var $show _connect_error=1; var $use _usleep=0; / /change to 1 for faster execution //don ' t change to 1 in Windows servers unless you have PHP 5 var $sleeptime = 125000; var $loginsleeptime = 1000000; var $fp =null; VAR $loginprompt; VAR $conn 1; VAR $conn 2; * 0 = success 1 = couldn ' t open network connection 2 = UNK Nown host 3 = Login Failed 4 = PHP version too low / function Con Nect ($server, $user, $pass) { $RV =0; $vers =explode ('. ', php_version) ; $needvers =array (4,3,0); $j =count ($vers); $k =count ($needvers); if ($k< $j) $j = $k; for ($i =0 $i < $j $i + +) { if ($vers [$i]+0) > $nee dvers[$i]) break; if ($vers [$i]+0) < $needvers [$i]) { $this->connecterror (4); return 4; { $this->dis Connect (); if (strlen ($server)) { Preg_match ('/[^0-9 .] /', $server)) { $IP =gethostbyname ($server); &N Bsp if ($ip = = $server) { $IP = ''; $RV = 2;   } } else $ip = $server; } else $ip = ' 127.0.0.1 '; if (strlen ($IP)) { if ($this->fp=fsockopen ($ip, 23)) { fputs ($this->fp, $this->conn1); $this->sleep (); fputs ($this->fp, $this->conn2); $this->sleep (); $this->getresponse ($r); $r =explode ("n", $r); $this->loginprompt= $r [Count ($r)-1]; fputs ($this->fp, "$usern"); &NBSP; $this->sleep (); fputs ($this->fp, "$passn"); if ($this->use_usleep) usleep ($this->loginsleeptime); Else sleep (1); $this->getresponse ($r); $r =explode ("n", $r); if ($r [count ($r) -1]== ') | | ($this->loginprompt== $r [Count ($r)-1])) { $RV =3 & nbsp $this->disconnect (); } else $RV = 1; } if ($RV) $this->connecterror ($RV); &nbsP return $RV; } function Disconnect ($exit =1) { if ($this->fp) { if ($exit) $this->docommand (' exit ', $junk); fclose ($this->FP); $this->fp=null; } function DoCommand ($c,& $r) { & nbsp if ($this->fp) { fputs ($this->fp, "$CN"); &NBSP ; $this->sleep (); $this->getresponse ($r); $r =preg_replace ("/^.*?n (. *) n[^n]*$/", "$", $r); } return $this->fp?1:0; } function GetResponse (& $r) { $r = '; do { $r. =fread ($this->FP, 1024); &NB Sp $s =socket_get_status ($this->fp); } while ($s [' unread_bytes ']); } function sleep () { if ($this->use_usleep) usleep ($this ->sleeptime); Else sleep (1); } function phptelnet () { $this->CONN1=CHR (0xFF) Chr (0xFB ). chr (0x1F). chr (0xFF). chr (0xFB). chr (0x20) Chr (0xFF) Chr (0xFB). chr (0x18). chr (0xFF). chr (0xFB). chr (0x27) Chr (0xFF) Chr (0xFD). chr (0x01). chr (0xFF). chr (0xFB). chr (0x03) Chr (0xFF) Chr (0xFD). chr (0x03). chr (0xFF). chr (0xFC). chr (0x23) Chr (0xFF) Chr (0xFC). chr (0x24). chr (0xFF). chr (0xFA).   CHR (0x1F) Chr (0x00). chr (0x50). chr (0x00). chr (0x18). chr (0xFF). chr (0xF0) Chr (0xFF). chr (0xFA). chr (0x20). chr (0x00). chr (0x33). chr (0x38) Chr (0x34). chr (0x30). chr (0x30). chr (0x2c). chr (0x33). chr (0x38) Chr (0x34). chr (0x30). chr (0x30). chr (0xFF). chr (0xF0). chr (0xFF) Chr (0xFA) Chr (0x27) Chr (0x00). chr (0xFF). chr (0xF0). chr (0xFF) Chr (0xFA) Chr (0x18). chr (0x00). chr (0x58). chr (0x54). chr (0x45) Chr (0x52) Chr (0x4d). chr (0xFF). chr (0xF0); $this->CONN2=CHR (0xFF). chr (0xFC) Chr (0x01). chr (0xFF). chr (0xFC). chr (0x22) Chr (0xFF) Chr (0xFE) Chr (0x05). chr (0xFF). chr (0xFC). chr (0x21); } function Connecterror ($num) { IF ($this->show_connect_error) switch ($num) { case 1:echo ' <br/>[php Telnet] <a href= "http://www.jb51.net/php-telnet/errors/fsockopen.php" >connect failed:unable to open Network connection</ A><br/> '; Break case 2:echo ' <br/>[php Telnet] <a href= ' http://www.jb51.net/php-telnet/errors/unkn own-host.php ">connect failed:unknown host</a><br/>"; Break case 3:echo ' <br/>[php Telnet] <a href= ' Http://www.jb51.net/php-telnet/errors/logi n.php ">connect failed:login failed</a><br/>"; Break case 4:echo ' <br/>[php Telnet] <a href= ' http://www.jb51.net/php-telnet/errors/php- version.php ">connect failed:your server ' s PHP version is too low for php telnet</a><br/> '; Break } }} ?>