PHP Checking if a port is Active
<p>I'm in the process of creating my own service status script as both a chance to become more familiar with the PHP language and to design it from the ground up as being as efficient as possible for my needs.</p>

<p>A section of my code used in both my cron job and testing a connection parts queries the IP/Port of a service to make sure it is online. My issue is that the script simply queries whether the port is "Unblocked" on that IP so if for instance I was querying port 21 with an FTP server and that FTP server crashed my script would not detect any changes meaning its not doing what I want it to do. Instead I would be wanting the IP and port to be queried and for my script to see if there is actually something running on that port, if there is show online if not error out. I've had a look on google and it seems like I would have to send a packet/receive a response so PHP can tell there's something active? I'm not sure.</p>

<p>This is my current code below:</p>

<pre><code>&lt;?php
    $host = $_POST['servip'];
    $port = $_POST['servport'];

    if (!$socket = @fsockopen($host, $port, $errno, $errstr, 3)) {
        echo "Offline!";
    } else {
        echo "Online!";
        fclose($socket);
    }
?&gt;
</code></pre>