• 13th June 2007 -By Vinu Thomas

    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.

  • 8 Comments to “PHP port check”

    • PHP Script - Open port check on November 16, 2007

      [...] this from Vinu’s blog. This simple script will attempt to connect to the site:port using fsockopen to determine if a [...]

    • Vince on February 17, 2008

      I know this is posting is just a tad old, but I got here by way of a Google search and found exactly what I was looking for. Anyway, I wanted to add that if the script fails to connect you get a PHP error about the connection being refused. So if you were wanting to use this script on a webpage, that could be a bit unsightly. To get the point, you can suppress the error message by adding this to the script:

      error_reporting(0);

      Thanks for doing the hard part, though.

    • vinu on February 22, 2008

      Hey Vince – thanks for pointing that out. Updated the code to include that….

    • Claudio on October 21, 2008

      Awesome!!
      Exactly the script/codes I needed in that moment! Thanks for sharing!

    • Cale on February 19, 2009

      Thanks Vinu. I needed this for monitoring a web service via a website.

    • Angel on August 1, 2009

      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 !

    • Dutch_com on October 12, 2009

      Thanks for sharing!… im not using it for a website check but for a service running on another port… and works flawless…..!!!

    • Dutch_com on October 12, 2009

      Thanks for sharing!… im not using it for a website check but for a service running on another port… and works flawless…..!!!

    Leave a Reply