How can I ping a server port with PHP

0 votes

I want a PHP script which allows you to ping an IP address and a port number (ip:port). I found a similar script but it works only for websites, not ip:port.

<?php

function ping($host, $port, $timeout) 
{ 
  $tB = microtime(true); 
  $fP = fSockOpen($host, $port, $errno, $errstr, $timeout); 
  if (!$fP) { return "down"; } 
  $tA = microtime(true); 
  return round((($tA - $tB) * 1000), 0)." ms"; 
}

//Echoing it will display the ping if the host is up, if not it'll say "down".
echo ping("www.google.com", 80, 10);

?>

The idea is that I can type in the IP address and port number, and I get the ping response

Nov 22, 2020 in PHP by kartik
• 37,520 points
10,442 views

1 answer to this question.

0 votes

Hello,

Try this :

echo exec('ping -n 1 -w 1 72.10.169.28');

Hope it helps!!

answered Nov 22, 2020 by Niroj
• 82,840 points

Related Questions In PHP

0 votes
0 answers

How can I parse a JSON file with PHP?

I want to parse a JSON file ...READ MORE

Jun 17, 2022 in PHP by narikkadan
• 63,600 points
435 views
0 votes
1 answer

Connection with MySQL server using PHP. How can we do that?

Hey @kartik, You have to provide MySQL hostname, ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,840 points
1,296 views
0 votes
1 answer

How can we track Google Analytics on a redirection page with PHP?

Hello @kartik, Since the page that is sending ...READ MORE

answered Apr 29, 2020 in PHP by Niroj
• 82,840 points
1,183 views
0 votes
1 answer

How can I expire a PHP session after 50 minutes?

Hii @kartik, Use the session_set_cookie_params funciton to set the session. If necessary call this function ...READ MORE

answered May 19, 2020 in PHP by Niroj
• 82,840 points
1,274 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,012 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
2,995 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
2,918 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
44,696 views
0 votes
1 answer

How can I ping a server port with PHP?

Hello @kartik, Test different ports: $wait = 1; // ...READ MORE

answered Nov 19, 2020 in PHP by Niroj
• 82,840 points
3,858 views
0 votes
1 answer

How can I use Sockets.io on the client side and communicate with a PHP based application on the server?

Hello @kartik, For 'long-lived connection' , you can ...READ MORE

answered Aug 24, 2020 in PHP by Niroj
• 82,840 points
3,035 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP