PHP test to check whether a port is open or not

You need to replace the
$address =”the_test_address.com”;
$port = ‘port_number’;

/*Code starts*/
/*port test*/

<?php
$address=”smtp.gmail.com”;
$port = ‘456’;
if (isset($port) and
($socket=socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) and
(socket_connect($socket, $address, $port)))  {
$text=”Connection successful on IP $address, port $port”;
socket_close($socket);
}
else  {
$text=”Unable  connect<pre>”.socket_strerror(socket_last_error()).”</pre>”;
}
echo “<html><head></head><body>”.
$text.
“</body></html>”;
?>

/*code ends*/
code courtesy: http://php.net/socket_connect

How to test :- Copy this code to a file say, porttest.php, change the $address , $port to the required and put it somewhere accessible by your server , like /var/www/ in linux. And run the same by opening localhost/porttest.php

Advertisements

One thought on “PHP test to check whether a port is open or not

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s