PHP to determine whether the server port is open code

Source: Internet
Author: User
Tags server port

Record, spare

 
 
  1. <?php
  2. $host = ' www.google.com '; To ping the address, can also be IP
  3. $port = ' 80 '; The port to Ping
  4. $num = 3;
  5. function Microtime_float ()
  6. {
  7. List ($usec, $sec) = Explode ("", Microtime ());
  8. Return ((float) $usec + (float) $sec);
  9. }
  10. function ping ($host, $port)
  11. {
  12. $time _start = Microtime_float ();
  13. $ip = gethostbyname ($host);
  14. $fp = @fsockopen ($host, $port,& $errno,& $errstr, 1);
  15. if (! $fp) return to ' Replay time out! ';
  16. $get = "Get/http/1.1\r\nhost:" $host. " \r\nconnection:close\r\n\r\n ";
  17. @fputs ($fp, $get);
  18. @fclose ($FP);
  19. $time _end = Microtime_float ();
  20. $time = $time _end-$time _start;
  21. $time = Ceil ($time * 1000);
  22. Return ' Reply from ' $ip. ': Time= '. $time. ' Ms<br/> ';
  23. }
  24. Echo ' Pinging '. $host. ' ['. gethostbyname ($host). '] With Port: '. $port. ' Data:<br/><br/> '. \ r \ n ";
  25. for ($i = 0; $i < $num; $i + +)
  26. {
  27. Ping ($host, $port);
  28. Sleep (1);
  29. Ob_flush ();
  30. Flush ();
  31. }
  32. ?>

This digest is from http://www.cnblogs.com/luoine/archive/2010/12/01/1893156.html




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.