Pyroglyph Pyroglyph - 1 month ago 3
Apache Configuration Question

See if a server is listening on a port in PHP?

Before answering, please note that I am completely new to PHP. I have heard it is powerful.

What I'm trying to do is have a page on my (Apache 2) web server that when a user clicks a button on the page, the server will check if a port is running on it's own IP with a preset port for each button, I want multiple buttons with the same IP but different ports to be pinged.


  • Button 1 (Terraria Server) is clicked, server pings and tells the user if it gets a response or not.

  • Button 2 (Minecraft Server) is clicked, server pings and tells the user etc etc.

I already have PHP installed and working, all I need is some code :)


Attempt a connection on the port and return the result:

     function Connect($port) {
        $serverConn = @stream_socket_client("tcp://{$port}", $errno, $errstr);
        if ($errstr != '') {
            return false;
       return true;

       switch ($_POST['portTest']){
           case 'minecraft': $port= '25565';
           case 'Terraria': $port= '7777';
           default: exit;
     if (Connect($port)){
         echo "Server is running!";
         echo "Server is down";

    <form method="POST">
    <input type="submit" name="portTest" value="minecraft">
    <input type="submit" name="portTest" value="Terraria">