Logo Background RSS

PHP port check

  • Written by vinuvinu 3 Comments3 Comments Comments
    Last Updated: June 13th, 2007

    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.

    Bookmark and share:
    • del.icio.us
    • Digg
    • StumbleUpon
    • BlinkList
    • blogmarks
    • Furl
    • Slashdot
    • Spurl
    • Technorati
    • YahooMyWeb
    • description
    • Facebook
    • Google
    • Live
    • Ma.gnolia
    • NewsVine
    • Reddit
    • TwitThis

Advertisement

  1. #1 Vince
    February 17th, 2008 at 5:22 pm

    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.

    Post ReplyPost Reply
  2. #2 vinu
    February 22nd, 2008 at 7:59 pm

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

    Post ReplyPost Reply
  • Trackback: PHP Script - Open port check Trackbacks
  • Leave a Comment