Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I ping a server port with PHP?

Tags:

php

ping

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);  ?> 

I want this for a game server.

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

like image 891
user1288533 Avatar asked Mar 23 '12 15:03

user1288533


People also ask

How do I ping a port in php?

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".

How do I ping a php server?

In PHP, the ping command is exactly the same as in terminal/command prompt, only it is executed within the exec() function. The exec() is an in-built PHP function that enables the execution of an external program.

How do I ping a server with a port number?

The easiest way to ping a specific port is to use the telnet command followed by the IP address and the port that you want to ping. You can also specify a domain name instead of an IP address followed by the specific port to be pinged. The “telnet” command is valid for Windows and Unix operating systems.

Can you ping test a port?

You can't ping ports, as Ping is using ICMP which is an internet layer protocol that doesn't have ports. Ports belong to the transport layer protocols like TCP and UDP.


2 Answers

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 = '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.

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 {         fclose($file);         $status = ($stoptime - $starttime) * 1000;         $status = floor($status);     }     return $status; } 
like image 85
ShadowScripter Avatar answered Sep 23 '22 18:09

ShadowScripter


In case the OP really wanted an ICMP-Ping, there are some proposals within the User Contributed Notes to socket_create() [link], which use raw sockets. Be aware that on UNIX like systems root access is required.

Update: note that the usec argument has no function on windows. Minimum timeout is 1 second.

In any case, this is the code of the top voted ping function:

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;     }     socket_close($socket);     return $result; } 
like image 33
user1931751 Avatar answered Sep 21 '22 18:09

user1931751