PHP code used to determine whether a server port is opened

Source: Internet
Author: User
Record it, and use it later? Php $ host = 'www .google.com '; // The IP address to be pinged, or IP $ port = '80'; // port $ num = 3 to be pinged; functionmicrotime_float () {list ($ usec, $ sec) = explode (, microti

Record it and use it later

 
 
  1. $ Host = 'www .google.com '; // The IP address to be pinged. It can also be an IP address.
  2. $ Port = '80'; // port to be pinged
  3. $ Num = 3;
  4. Function microtime_float ()
  5. {
  6. List ($ usec, $ sec) = explode ("", microtime ());
  7. Return (float) $ usec + (float) $ sec );
  8. }
  9. Function ping ($ host, $ port)
  10. {
  11. $ Time_start = microtime_float ();
  12. $ Ip = gethostbyname ($ host );
  13. $ Fp = @ fsockopen ($ host, $ port, & $ errno, & $ errstr, 1 );
  14. If (! $ Fp) return 'replay time out! ';
  15. $ Get = "GET/HTTP/1.1 \ r \ nHost:". $ host. "\ r \ nConnection: Close \ r \ n ";
  16. @ Fputs ($ fp, $ get );
  17. @ Fclose ($ fp );
  18. $ Time_end = microtime_float ();
  19. $ Time = $ time_end-$ time_start;
  20. $ Time = ceil ($ time * 1000 );
  21. Return 'reply from '. $ ip.': time = '. $ time.' ms ';
  22. }
  23. Echo 'pinging'. $ host. '['. gethostbyname ($ host). '] with Port:'. $ port. 'of data: '. "\ R \ n ";
  24. For ($ I = 0; $ I <$ num; $ I ++)
  25. {
  26. Ping ($ host, $ port );
  27. Sleep (1 );
  28. Ob_flush ();
  29. Flush ();
  30. }
  31. ?>

This article 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.