PHP port check
There was a request from the PHP Bangalore User group on how to check if a webserver is running on a domain. The solution I proposed was to check if we can connect to port 80 on that domain. That would let us know if the webserver on the domain was running or not. Here’s the code snippet showing how to do that using PHP Sockets.
< ?php
error_reporting(0);
// Your Domain to check
$site = "www.vinuthomas.com";
// Port to check - Default port 80 for webserver
// You can check other ports by changing the
// value of $port
$port = 80;
//open the port and check
$fp = fsockopen($site,$port,$errno,$errstr,10);
if(!$fp)
{
echo "Cannot connect to server";
// you can send your notification mail here.
}else{
echo "Connect was successful - no errors";
fclose($fp);
}
?>
This script can be modified to check for other ports on the server like FTP and SMTP by just changing the $port value in the script.


