PHP implementation of the Ping port function instance, phpping Port Example _php Tutorial

Source: Internet
Author: User
Tags chop php programming

Example of a ping port function implemented by PHP, phpping Port instance


This article describes the ping port function implemented by PHP. Share to everyone for your reference.

The specific implementation code is as follows:

Copy CodeThe code is as follows:
<?php
/*
* @author Xujiajay
* @date 2010-10-7
* @function PHP functions that can ping ports
*
*/
Error_reporting (E_error);
Header ("content-type:text/html; Charset=utf-8 ");
Set_time_limit (120);
$host = isset ($_post[' url ')? Chop (Str_replace (' http://', ' ', $_post[' url ')), ' www.baidu.com ';
$port = isset ($_post[' Duankou ')? Chop ($_post[' Duankou '): ' 80 ';
$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 ' unknown ';
$get = "Get/http/1.1\r\nhost:". $host. " \r\nconnection:close\r\n\r\n ";
@fputs ($fp, $get);
$data = ";
while ($fp &&!feof ($fp))
$data. = Fread ($fp, 1024);
@fclose ($FP);
$array = explode ("\ n", $data);
$k = 2;
for ($i = 0; $i < $i + +)
{
if (Stristr ($array [$i], ' Server ') {$k = $i; break;}
}
if (!stristr ($array [$k], ' Server ')) return ' unknown ';
else return str_replace (' server ', ' Server Software ', $array [$k]);
}
function ping ($host, $port)
{
$time _start = Microtime_float ();
$ip = gethostbyname ($host);
$fp = @fsockopen ($host, $port,& $errno,& $errstr, 1);
if (! $fp) return ' Request timed out. ' \ 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 ' Reply from ' $ip. ': Time= '. $time. ' MS ';
}
if (isset ($_post[' url ')) && isset ($_post[' Duankou '))
{
Echo '. Getsoft ($host, $port). ";
Echo ' Pinging '. $host. ' ['. gethostbyname ($host). '] With Port: '. $port. ' of data: '. ' \ r \ n ";
Ob_flush ();
Flush ();
for ($i = 0; $i < $num; $i + +)
{
Echo Ping ($host, $port);
Ob_flush ();
Flush ();
Sleep (1);
}
}
?>

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/910606.html www.bkjia.com true http://www.bkjia.com/PHPjc/910606.html techarticle PHP Implementation of the Ping port function instance, phpping Port instance This article describes the PHP implementation of the ping port function. Share to everyone for your reference. The specific implementation code is as follows: Copy code ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.