13 June 2007 View Comments

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.

  • Dutch_com
    Thanks for sharing!... im not using it for a website check but for a service running on another port... and works flawless.....!!!
  • Thanks Vinu. I needed this for monitoring a web service via a website.
  • Angel
    Thank you :) I'm looking for something 'almost' like this.
    I'll try to explain: I regularly use servers that require a working 'ident' (port 113). When I can't connect to a server, first thing I do is go here to test if my ident is truly working: http://www.high-society.at/services.php
    then click on 'ident-check'.
    If my ident is working it will reply with my ident@my.ip
    Like this:
    Your ident daemon replied with user myident (Operating System UNIX).
    Your full IP mask is therefore: myident@423.567.44

    If it's not working it will check for 30 seconds then reply: Error!
    The connection was refused ..........etc

    Of course you need to have:
    1. Forwarded port 113 in your router
    2. An ident running while performing this test.
    The easiest way is to use an FTP tool like FlashFXP or FTPRush and enter your ident name in: Tools/Options/Connection/Ident/ check 'Enable Ident', then enter a name in Ident name, Apply/OK
    Leave FTPRush open while you do the test. It will reply in the it's window like this during the test: [i] Ident Request from 79.140.35.203
    Which of course is that site checking.

    I would like to have my own web-page that does exactly the same thing just in case that site isn't there any there longer.

    Thank you :D !
  • Awesome!!
    Exactly the script/codes I needed in that moment! Thanks for sharing!
  • vinuthomas
    Hey Vince - thanks for pointing that out. Updated the code to include that....
blog comments powered by Disqus