Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if port open and forwarded using PHP

Tags:

Full Disclosure: There's a similar question here.

Is there any way I can test if a particular port is open and forwarded properly using PHP? Specifically, how do I go about using a socket to connect to a given user with a given port?

An Example of this is in the 'Custom Port Test' section of WhatsMyIP.org/ports.

like image 760
Charlie Salts Avatar asked Feb 09 '10 02:02

Charlie Salts


People also ask

How check port is open or not in PHP?

A quick tutorial on how to check for open ports using PHP. This function fsockopen() is used in the Open Port Check Tool in this blog. You can check whether a certain port is open on a specified IP address or hostname.

How do you check if port forward is open?

After you have successfully forwarded your ports, you will want to check to see if they are forwarded correctly. You can use this tool to see if your ports are open correctly: www.portchecktool.com. This tool will check for open ports and see if there are any services responding on that port.

What port is PHP listening on?

Remote Server By default, this configuration starts a PHP-FPM server listening on port 9000 that binds to 127.0. 0.1 (localhost).

What is a port check?

The open port checker is a tool you can use to check your external IP address and detect open ports on your connection. This tool is useful for finding out if your port forwarding is setup correctly or if your server applications are being blocked by a firewall.


1 Answers

I'm not sure what you mean by being "forwarded properly", but hopefully this example will do the trick:

$host = 'stackoverflow.com'; $ports = array(21, 25, 80, 81, 110, 443, 3306);  foreach ($ports as $port) {     $connection = @fsockopen($host, $port);      if (is_resource($connection))     {         echo '<h2>' . $host . ':' . $port . ' ' . '(' . getservbyport($port, 'tcp') . ') is open.</h2>' . "\n";          fclose($connection);     }      else     {         echo '<h2>' . $host . ':' . $port . ' is not responding.</h2>' . "\n";     } } 

Output:

stackoverflow.com:21 is not responding. stackoverflow.com:25 is not responding. stackoverflow.com:80 (http) is open. stackoverflow.com:81 is not responding. stackoverflow.com:110 is not responding. stackoverflow.com:443 is not responding. stackoverflow.com:3306 is not responding. 

See http://www.iana.org/assignments/port-numbers for a complete list of port numbers.

like image 73
Alix Axel Avatar answered Sep 29 '22 23:09

Alix Axel