Example code of ping port function implemented in PHP

怪我咯
Release: 2023-03-14 07:18:02
Original
1856 people have browsed it

Ping is a command under Windows, Unix and Linux systems. Ping also belongs to a communication protocol and is part of the TCP/IP protocol. You can use the "ping" command to check whether the network is connected, which can help us analyze and determine network faults. Application format: Ping space IP address. This command can also be used with many parameters. Specifically, type Ping and press Enter to see detailed instructions.

PING (Packet Internet Groper), Internet packet explorer, a program used to test the amount of network connections. Ping sends an ICMP (Internet Control Messages Protocol), which is the Internet Message Control Protocol; an echo request message is sent to the destination and reports whether the desired ICM Echo (ICMP echo response) is received. It is a command used to check whether the network is smooth or the network connection speed. As an administrator or hacker living on the network, the ping command is the first DOS command that must be mastered. The principle it uses is this: using the uniqueness of the IP address of the machine on the network, send a message to the target IP address. A data packet, and then ask the other party to return a data packet of the same size to determine whether the two network machines are connected and what the delay is.

Ping refers to end-to-end connectivity, which is usually used as an availability check. However, some viruses and Trojans will force a large number of remote execution ping commands to seize your network resources, causing the system to slow down and the network speed to slow down. . Strictly prohibit ping intrusion as a basic function of most firewalls for users to choose. Under normal circumstances, if you are not using it as a server or conducting network testing, you can safely select it to protect your computer.

This article mainly introduces the ping port function implemented by PHP, and analyzes in detail the skills of PHP using socket programming in the form of examples. Friends in need can refer to it. The specific implementation code is as follows:

<?php
/*
 * @function   可以ping端口的php函数
 *
 */
    error_reporting(E_ERROR);
    header("content-Type: text/html; charset=utf-8");
    set_time_limit(120);
    $host = isset($_POST[&#39;url&#39;]) ? chop(str_replace(&#39;http://&#39;,&#39;&#39;,$_POST[&#39;url&#39;])) : &#39;www.baidu.com&#39;;
    $port = isset($_POST[&#39;duankou&#39;]) ? chop($_POST[&#39;duankou&#39;]) : &#39;80&#39;;
    $num  = 10;
    function microtime_float()
    {
            list($usec, $sec) = explode(" ", microtime());
            return ((float)$usec + (float)$sec);
    }
    function getsoft($host,$port)
    {
            $fp = @fsockopen($host,$port,&$errno,&$errstr,3);
            if(!$fp) return &#39;unknown&#39;;
            $get = "GET / HTTP/1.1\r\nHost:".$host."\r\nConnection: Close\r\n\r\n";
            @fputs($fp,$get);
            $data = &#39;&#39;;
            while ($fp && !feof($fp))
            $data .= fread($fp, 1024);
            @fclose($fp);
            $array = explode("\n",$data);
            $k = 2;
            for($i = 0;$i < 20;$i++)
            {
                    if(stristr($array[$i],&#39;Server&#39;)){$k = $i; break;}
            }
            if(!stristr($array[$k],&#39;Server&#39;)) return &#39;unknown&#39;;
            else return str_replace(&#39;Server&#39;,&#39;服务器软件&#39;,$array[$k]);
    }
    function ping($host,$port)
    {
            $time_start = microtime_float();
            $ip = gethostbyname($host);
            $fp = @fsockopen($host,$port,&$errno,&$errstr,1);
            if(!$fp) return &#39;Request timed out.&#39;."\r\n";
            $get = "GET / HTTP/1.1\r\nHost:".$host."\r\nConnection: Close\r\n\r\n";
            @fputs($fp,$get);
            @fclose($fp);
            $time_end = microtime_float();
            $time = $time_end - $time_start;
            $time = ceil($time * 1000);
            return &#39;Reply from &#39;.$ip.&#39;: time=&#39;.$time.&#39;ms&#39;;
    }
    if(isset($_POST[&#39;url&#39;]) && isset($_POST[&#39;duankou&#39;]))
    {
            echo &#39;<font color="#FF0000">&#39;.getsoft($host,$port).&#39;</font>&#39;;
            echo &#39;Pinging &#39;.$host.&#39; [&#39;.gethostbyname($host).&#39;] with Port:&#39;.$port.&#39; of data:&#39;."\r\n";
            ob_flush();
            flush();
            for($i = 0;$i < $num;$i++)
            {
                    echo ping($host,$port);
                    ob_flush();
                    flush();
                    sleep(1);
            }
    }
?>
<form method="POST">
域名/IP:<input type="text" name="url" value="<?php echo $host;?>" size="50"> 
端口:<input type="text" name="duankou" value="<?php echo $port;?>" size="10"> 
<input type="submit" value="ping">
</form>
Copy after login

The above is the detailed content of Example code of ping port function implemented in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!