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.

</li> <li>

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

</li> </ul>

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


Attempt a connection on the port and return the result:

<?php function Connect($port) { $serverConn = @stream_socket_client("tcp://{$port}", $errno, $errstr); if ($errstr != '') { return false; } fclose($serverConn); return true; } if(isset($_POST['portTest'])){ switch ($_POST['portTest']){ case 'minecraft': $port= '25565'; break; case 'Terraria': $port= '7777'; break; default: exit; } if (Connect($port)){ echo "Server is running!"; }else{ echo "Server is down"; } } ?> <form method="POST"> <input type="submit" name="portTest" value="minecraft"> <input type="submit" name="portTest" value="Terraria"> </form>


  • Element IDs are not being recognised and are not triggering function
  • VB.Net Simple Json Edit
  • batch outputting with space after each string
  • Launch Minecraft from the command line Python/C++
  • Minecraft One Player Per Config
  • How to override a fullscreen game with keywindow borderless window (overlay)?
  • Running a if statement only once in pygame
  • How to position a html element under a fixed div
  • fwrite() not permitted, but phpinfo() says it should be
  • Flat Terrain Generation in Java
  • Get absolute desktop path
  • Android Studio 3.0 Canary 1 - Simulator stuck on boot
  • Changing column based on header ComboBox selection
  • How to deploy react with express to heroku
  • How to get OS version and Device Name of an Android device connected via USB with C#.net (VS2010/4.0
  • BBC micro:bit - Radio string transfer random carriage returns
  • Check for stdout or stderr
  • docker-compose: connection refused between containers, but service accessible from host
  • Unable to get CAP_CHOWN and CAP_DAC_OVERRIDE working for regular user
  • change color of jstree node
  • Creating a Multi-Step Modal Using Jquery
  • Subclassing QGraphicsItem prevents me from being able to use itemAt() on a QGraphicsScene/View
  • Selenium to click on a javascript button corresponding to a text
  • Detect when Facebook like button is clicked
  • Debug.DrawLine not showing in the GameView
  • Android full screen on only one activity?
  • Android fill_parent issue
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Android screen density dpi vs ppi
  • Array.prototype.includes - not transformed with babel
  • Bug in WPF DataGrid
  • Resize panoramic image to fixed size
  • Volusion's generic SQL folder, functionality
  • Adding custom controls to a full screen movie
  • jquery mobile loadPage not working
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do you join a server to an Active Directory (domain)?
  • coudnt use logback because of log4j
  • java string with new operator and a literal