How to Ping a Server Port With PHP

How can I ping a server port with PHP?

I think the answer to this question pretty much sums up the problem with your question.

If what you want to do is find out whether a given host will accept
TCP connections on port 80, you can do this:

$host = ''; 
$port = 80;
$waitTimeoutInSeconds = 1;
if($fp = fsockopen($host,$port,$errCode,$errStr,$waitTimeoutInSeconds)){
// It worked
} else {
// It didn't work

For anything other than TCP it will be more difficult (although since
you specify 80, I guess you are looking for an active HTTP server, so
TCP is what you want). TCP is sequenced and acknowledged, so you will
implicitly receive a returned packet when a connection is successfully
made. Most other transport protocols (commonly UDP, but others as
well) do not behave in this manner, and datagrams will not be
acknowledged unless the overlayed Application Layer protocol
implements it.

The fact that you are asking this question in this manner tells me you
have a fundamental gap in your knowledge on Transport Layer protocols.
You should read up on ICMP and TCP, as well as the OSI Model.

Also, here's a slightly cleaner version to ping to hosts.

// Function to check response time
function pingDomain($domain){
$starttime = microtime(true);
$file = fsockopen ($domain, 80, $errno, $errstr, 10);
$stoptime = microtime(true);
$status = 0;

if (!$file) $status = -1; // Site is down
else {
$status = ($stoptime - $starttime) * 1000;
$status = floor($status);
return $status;

PHP ping IP:Port

You must use socket_create() since ping uses ICMP and fsockopen is only for TCP and UDP. ICMP echo packets do not specify any port. In the socket_create manual, in the user comments there are several ping() example implementations.

how to ping ip addresses in php and give results

$ip =   "";
exec("ping -n 3 $ip", $output, $status);

output looks like below

[0] =>
[1] => Pinging with 32 bytes of data:
[2] => Reply from bytes=32 time<1ms TTL=128
[3] => Reply from bytes=32 time<1ms TTL=128
[4] => Reply from bytes=32 time<1ms TTL=128
[5] =>
[6] => Ping statistics for
[7] => Packets: Sent = 3, Received = 3, Lost = 0 (0% loss),
[8] => Approximate round trip times in milli-seconds:
[9] => Minimum = 0ms, Maximum = 0ms, Average = 0ms

Pinging a server in php without the port?

ICMP doesn't use a port.

You might want to use exec() to ping the server. Check out: Pinging an IP address using PHP and echoing the result

Cant get php script to ping specific port and IP

If I set the port to 80, it works but any other port it says it not working.

This makes it look pretty much like a firewall problem (either on the generating end or at the receiving end).

Are you sure the server where this code's running is allowing any communication at all except to a restricted range of ports? How about ports 25, 81, 110, 443 and 8080?

What port to ping on windows machine to check if it is online?

ref: PHP: socket_create - Manual

function ping($host, $timeout = 1) {
/* ICMP ping packet with a pre-calculated checksum */
$package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";
$socket = socket_create(AF_INET, SOCK_RAW, 1);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0));
socket_connect($socket, $host, null);

$ts = microtime(true);
socket_send($socket, $package, strLen($package), 0);
if (socket_read($socket, 255))
$result = microtime(true) - $ts;
else $result = false;

return $result;

Pinging an IP address using PHP and echoing the result

NOTE: Solution below does not work on Windows. On linux exec a "which ping" command from the console, and set command path (of the suggested exec call) accordingly

I think you want to check the exit status of the command, whereas shell_exec gives you full output (might be dangerous shall command output change from command version to version. for some reason). Moreover your variable $ip is not interpreted within single quotes. You'd have to use double ones "". That might be the only thing you need to fix in order to make it work.

But I think following code can be more "portable". IMHO it is in fact better to catch the exit status, rather than trying to parse result string. IMHO it's also better to specify full path to ping command.

function pingAddress($ip) {
$pingresult = exec("/bin/ping -n 3 $ip", $outcome, $status);
if (0 == $status) {
$status = "alive";
} else {
$status = "dead";
echo "The IP address, $ip, is ".$status;


Related Topics

Leave a reply