请参见如果服务器是PHP的一个端口上侦听? [英] See if a server is listening on a port in PHP?

查看:224
本文介绍了请参见如果服务器是PHP的一个端口上侦听?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在回答之前,请注意,我是完全新的PHP。我听说它是​​强大的。

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

我试图做的是有我(的Apache 2)Web服务器上的页面,当用户点击网页上的按钮时,服务器会检查是否一个端口是在它自己的IP上运行了preSET端口每个按钮,我想用相同IP的多个按钮,但不同的端口被ping通。

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.

例如:


  • 按钮1(界面完全音频服务器)被点击,服务器ping 127.0.0.1:7777并告诉用户,如果它得到响应与否。

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

按钮2(我的世界服务器)被点击,服务器ping 127.0.0.1:25565并告诉用户等等等等。

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

我已经安装了PHP和工作,我需要的是一些code:)

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://127.0.0.1:$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>

这篇关于请参见如果服务器是PHP的一个端口上侦听?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆