PHP Open Port Tester

PHP Open Port Tester

PHP Open Port TesterOne of my favorite network tool websites is YouGetSignal.com and recently I was using their open port tester to ensure my port forwarding rules were working. I started wondering how difficult it would be to setup an open port tester using PHP and turns out it was really easy.

I’d like to share my basic working example below in case anyone has an interest or use for this as well. I did this using two files: portcheck.html and ports.php the port checker file is just a simple form that asks for the domain or host you want to test and then the port number you are testing. The action on this form points to the ports script which receives this request and then tests whether or not the port is open using the fsockopen PHP function.

Below is the source of portcheck.html:



<style>
		body, h2 { font-family: 'Lato', sans-serif; }<br />
	</style>

&nbsp;

<form action="ports.php"><label><strong>Domain/Host</strong></label>: <input name="host" type="text" />
<label><strong>Port</strong></label>: <input name="port" type="text" /><button type="submit">submit</button>

</form>

And below is the source of the ports.php script:



<style>
		body, h2 { font-family: 'Lato', sans-serif; }<br />
	</style>

&nbsp;

<!--?php &lt;br ?--> $host = $_REQUEST['host'];
$port = $_REQUEST['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";
}
?&gt;

Save both portcheck.html and ports.php in the same folder the visit portcheck.html in your browser. You should get a form that’s asking for a domain/host and a port number as shown in the screenshot below:
PHP Open Port Tester

If you entered in google.com and port 80 then click submit, the ports script will test connectivity using fsockopen on google.com and port 80 then return the results as shown below:
PHP Open Port Tester Results

I hope that someone finds this useful or interesting. Feel free to leave your comments and/or suggestions below. Thanks for reading!

DIGITAL PCI © 2018