0

I'm using the following to get status of and IP or a domain

<?php
 system('ping -c1 -w1 193.33.186.70') ;
?>

How would I ping port 80 tho? Returns nothing at all once port is supplied.. tried to add it to the end by ':80' and ' 80'

Any ideas appreaciated!

Saulius Antanavicius
  • 1,371
  • 6
  • 25
  • 55

4 Answers4

4

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 = '193.33.186.70';
$port = 80;
$waitTimeoutInSeconds = 1;
if ($fp = fsockopen($host,$port,$errCode,$errStr,$waitTimeoutInSeconds)) {
  // It worked
} else {
  // It didn't work
}
fclose($fp);

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.

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
2

Ping command is not using TCP protocol, but ICMP protocol. So you can't use TCP port 80.

You can use fsockopen() or cURL to ping a server and gethostbyaddr() to get a domain name.

Peter
  • 16,453
  • 8
  • 51
  • 77
1

Generally, you cannot ping a port: ping works over ICMP, which does not have a port abstraction.

If what you want to is just check whether a given port is active, use sockets or (for port 80) curl.

alf
  • 8,377
  • 24
  • 45
0

ping sends ICMP packets. To test a TCP port, you need to send TCP packets.

However, even opening and immediately closing a TCP connection is generally not what you want. Instead, just test the service you expect to be there there. For example, to test whether an HTTP server is functioning:

<?php
$ch = curl_init("http://www.example.com/");
curl_setopt($ch, CURLOPT_NOBODY, true);
$ping_result = curl_exec($ch);
curl_close($ch);
echo 'Service ' . ($ping_result ? 'up' : 'down');
phihag
  • 278,196
  • 72
  • 453
  • 469