怎么做到在官网显示在线人数 - 联机问答 - Minecraft(我的世界)中文论坛 -.html

怎么做到在官网显示在线人数 - 联机问答 - Minecraft(我的世界)中文论坛 -

Minecraft(我的世界)中文论坛

 找回密码
 注册(register)

!header_login!

只需一步,立刻登录

查看: 970|回复: 29
打印 上一主题 下一主题

[临时] 怎么做到在官网显示在线人数

 关闭 [复制链接]
men62466293 当前离线
积分
1562
帖子
主题
精华
贡献
爱心
钻石
人气
下界之星
最后登录
1970-1-1
注册时间
2018-6-12
查看详细资料
头像被屏蔽

来自:河北

跳转到指定楼层
楼主
 楼主| 发表于 2020-5-15 21:31:10 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
233金粒
如题,我想要在服务器官网中显示在线人数,使用Html,PHPstudy(用PHP是不是会很难...),就是这样的
没错,就是右边的那个17670个玩家在线那个!
有没有大佬告诉我怎么显示呀

最佳答案

查看完整内容

来自此处的代码 https://www.mcbbs.net/thread-781980-1-1.html index.php status.class.php php 是最好的语言!(
帖子永久链接: 
strings 当前离线
积分
26267
帖子
主题
精华
贡献
爱心
钻石
人气
下界之星
最后登录
1970-1-1
注册时间
2020-4-29
查看详细资料

来自:湖南

来自 2#
发表于 2020-5-15 21:31:11 | 只看该作者
来自此处的代码 https://www.mcbbs.net/thread-781980-1-1.html

        index.php
  1. <html>
  2. <head>
  3. <title>Minecraft server</title>
  4. </head>
  5. <body>
  6. <?php
  7.         include_once 'status.class.php';//引入<span style="color: rgb(65, 105, 225);"><font size="5"> </font><font size="3">status.class.php</font></span>
  8.         
  9.     $status = new MinecraftServerStatus(); // call the class
  10.     $response = $status-> getStatus('<font color="#4169e1">服务器IP</font>', <font color="#4169e1">服务器端口</font>);

  11. if(!$response) {
  12.     echo"The Server is offline!";
  13. } else {
  14.     echo"<img width="64" height="64" src="asas1.png".$response['favicon']."" /> <br>
  15.     服务器信息 ".'</br>'.'服务器IP'.$response['hostname']." 在线状态: ".$response['version']." 目前在线人数:".$response['players']."位玩家 最大在线人数: ".$response['maxplayers']."人".". 服务器版本:'".$response['motd']."'.
  16.     延迟:".$response['ping']." 毫秒.";
  17. }

  18. ?>
  19. </body>
  20. </html>
复制代码

status.class.php

  1. <?php
  2.     class MinecraftServerStatus {

  3.         private $timeout;

  4.         public function __construct($timeout = 2) {
  5.             $this->timeout = $timeout;
  6.         }

  7.         public function getStatus($host = '127.0.0.1', $version = '1.7.*' , $port = 25565) {

  8.             if (substr_count($host , '.') != 4) $host = gethostbyname($host);

  9.             $serverdata = array();
  10.             $serverdata['hostname'] = $host;
  11.             $serverdata['version'] = false;
  12.             $serverdata['protocol'] = false;
  13.             $serverdata['players'] = false;
  14.             $serverdata['maxplayers'] = false;
  15.             $serverdata['motd'] = false;
  16.             $serverdata['motd_raw'] = false;
  17.             $serverdata['favicon'] = false;
  18.             $serverdata['ping'] = false;

  19.             $socket = $this->connect($host, $port);

  20.             if(!$socket) {
  21.                 return false;
  22.             }

  23.             if(preg_match('/1.7|1.8/',$version)) {

  24.                 $start = microtime(true);

  25.                 $handshake = pack('cccca*', hexdec(strlen($host)), 0, 0x04, strlen($host), $host).pack('nc', $port, 0x01);

  26.                 socket_send($socket, $handshake, strlen($handshake), 0); //give the server a high five
  27.                 socket_send($socket, "\x01\x00", 2, 0);
  28.                 socket_read( $socket, 1 );

  29.                 $ping = round((microtime(true)-$start)*1000); //calculate the high five duration

  30.                 $packetlength = $this->read_packet_length($socket);

  31.                 if($packetlength < 10) {
  32.                     return false;
  33.                 }

  34.                 socket_read($socket, 1);

  35.                 $packetlength = $this->read_packet_length($socket);

  36.                 $data = socket_read($socket, $packetlength, PHP_NORMAL_READ);

  37.                 if(!$data) {
  38.                     return false;
  39.                 }

  40.                 $data = json_decode($data);

  41.                 $serverdata['version'] = $data->version->name;
  42.                 $serverdata['protocol'] = $data->version->protocol;
  43.                 $serverdata['players'] = $data->players->online;
  44.                 $serverdata['maxplayers'] = $data->players->max;

  45.                 $motd = $data->description;
  46.                 $motd = preg_replace("/(§.)/", "",$motd);
  47.                 $motd = preg_replace("/[^[:alnum:][:punct:] ]/", "", $motd);

  48.                 $serverdata['motd'] = $motd;
  49.                 $serverdata['motd_raw'] = $data->description;
  50.                 $serverdata['favicon'] = $data->favicon;
  51.                 $serverdata['ping'] = $ping;

  52.             } else {

  53.                 $start = microtime(true);

  54.                 socket_send($socket, "\xFE\x01", 2, 0);
  55.                 $length = socket_recv($socket, $data, 512, 0);

  56.                 $ping = round((microtime(true)-$start)*1000);//calculate the high five duration
  57.          
  58.                 if($length < 4 || $data[0] != "\xFF") {
  59.                     return false;
  60.                 }

  61.                 $motd = "";
  62.                 $motdraw = "";

  63.                 //Evaluate the received data
  64.                 if (substr((String)$data, 3, 5) == "\x00\xa7\x00\x31\x00"){

  65.                     $result = explode("\x00", mb_convert_encoding(substr((String)$data, 15), 'UTF-8', 'UCS-2'));
  66.                     $motd = $result[1];
  67.                     $motdraw = $motd;

  68.                 } else {

  69.                     $result = explode('§', mb_convert_encoding(substr((String)$data, 3), 'UTF-8', 'UCS-2'));
  70.                         foreach ($result as $key => $string) {
  71.                             if($key != sizeof($result)-1 && $key != sizeof($result)-2 && $key != 0) {
  72.                                 $motd .= '§'.$string;
  73.                             }
  74.                         }
  75.                         $motdraw = $motd;
  76.                     }

  77.                     $motd = preg_replace("/(§.)/", "", $motd);
  78.                     $motd = preg_replace("/[^[:alnum:][:punct:] ]/", "", $motd); //Remove all special characters from a string

  79.                     $serverdata['version'] = $result[0];
  80.                     $serverdata['players'] = $result[sizeof($result)-2];
  81.                     $serverdata['maxplayers'] = $result[sizeof($result)-1];
  82.                     $serverdata['motd'] = $motd;
  83.                     $serverdata['motd_raw'] = $motdraw;
  84.                     $serverdata['ping'] = $ping;

  85.             }

  86.             $this->disconnect($socket);

  87.             return $serverdata;

  88.         }

  89.         private function connect($host, $port) {
  90.             $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  91.             socket_connect($socket, $host, $port);
  92.             return $socket;
  93.         }

  94.         private function disconnect($socket) {
  95.             if($socket != null) {
  96.                 socket_close($socket);
  97.             }
  98.         }

  99.         private function read_packet_length($socket) {
  100.             $a = 0;
  101.             $b = 0;
  102.             while(true) {
  103.                 $c = socket_read($socket, 1);
  104.                 if(!$c) {
  105.                     return 0;
  106.                 }
  107.                 $c = Ord($c);
  108.                 $a |= ($c & 0x7F) << $b++ * 7;
  109.                 if( $b > 5 ) {
  110.                     return false;
  111.                 }
  112.                 if(($c & 0x80) != 128) {
  113.                     break;
  114.                 }
  115.             }
  116.             return $a;
  117.         }

  118.     }
  119. ?>
复制代码


php 是最好的语言!(
回复

使用道具 举报

men62466293 当前离线
积分
1562
帖子
主题
精华
贡献
爱心
钻石
人气
下界之星
最后登录
1970-1-1
注册时间
2018-6-12
查看详细资料
头像被屏蔽

来自:河北

板凳
 楼主| 发表于 2020-5-15 21:33:17 | 只看该作者
回复

使用道具 举报

men62466293 当前离线
积分
1562
帖子
主题
精华
贡献
爱心
钻石
人气
下界之星
最后登录
1970-1-1
注册时间
2018-6-12
查看详细资料
头像被屏蔽

来自:河北

地板
 楼主| 发表于 2020-5-15 21:34:16 | 只看该作者
图片在这!
回复

使用道具 举报

小天吖~ 当前离线
积分
7889
帖子
主题
精华
贡献
爱心
钻石
人气
下界之星
最后登录
1970-1-1
注册时间
2019-8-30
查看详细资料

来自:河北

5#
发表于 2020-5-15 21:34:52 | 只看该作者
https://www.mcbbs.net/forum.php?mod=viewthread&tid=487904&highlight=%E5%B7%A5%E5%85%B7
[网站工具]在服务器官方网站或论坛显示服务器的状态及人数!
https://www.mcbbs.net/thread-487904-1-1.html
(出处: Minecraft(我的世界)中文论坛)


回复

使用道具 举报

men62466293 当前离线
积分
1562
帖子
主题
精华
贡献
爱心
钻石
人气
下界之星
最后登录
1970-1-1
注册时间
2018-6-12
查看详细资料
头像被屏蔽

来自:河北

6#
 楼主| 发表于 2020-5-15 21:39:02 | 只看该作者
もぺもぺ 发表于 2020-5-15 21:35
来自此处的代码 https://www.mcbbs.net/thread-781980-1-1.html

        index.php

那怎么插入呢
回复

使用道具 举报

strings 当前离线
积分
26267
帖子
主题
精华
贡献
爱心
钻石
人气
下界之星
最后登录
1970-1-1
注册时间
2020-4-29
查看详细资料

来自:湖南

7#
发表于 2020-5-15 21:40:22 | 只看该作者

我认为这已经很明显了
回复

使用道具 举报

men62466293 当前离线
积分
1562
帖子
主题
精华
贡献
爱心
钻石
人气
下界之星
最后登录
1970-1-1
注册时间
2018-6-12
查看详细资料
头像被屏蔽

来自:河北

8#
 楼主| 发表于 2020-5-15 21:43:45 | 只看该作者
もぺもぺ 发表于 2020-5-15 21:40
我认为这已经很明显了

请加我的QQ:2271925039发给我
(这玩意复制下来就是一大坨堆在一起)
回复

使用道具 举报

strings 当前离线
积分
26267
帖子
主题
精华
贡献
爱心
钻石
人气
下界之星
最后登录
1970-1-1
注册时间
2020-4-29
查看详细资料

来自:湖南

9#
发表于 2020-5-15 21:44:25 | 只看该作者
men62466293 发表于 2020-5-15 21:43
请加我的QQ:2271925039发给我
(这玩意复制下来就是一大坨堆在一起)

使用鼠标选择,ctrl+C 复制
回复

使用道具 举报

men62466293 当前离线
积分
1562
帖子
主题
精华
贡献
爱心
钻石
人气
下界之星
最后登录
1970-1-1
注册时间
2018-6-12
查看详细资料
头像被屏蔽

来自:河北

10#
 楼主| 发表于 2020-5-15 21:47:23 | 只看该作者
もぺもぺ 发表于 2020-5-15 21:44
使用鼠标选择,ctrl+C 复制

Parse error: syntax error, unexpected '<' in G:\Desktop\test\index.php on line 10
回复

使用道具 举报

strings 当前离线
积分
26267
帖子
主题
精华
贡献
爱心
钻石
人气
下界之星
最后登录
1970-1-1
注册时间
2020-4-29
查看详细资料

来自:湖南

11#
发表于 2020-5-15 21:48:53 | 只看该作者
men62466293 发表于 2020-5-15 21:47
Parse error: syntax error, unexpected '

<html>
<head>
<title>Minecraft server</title>
</head>
<body>
<?php
        include_once 'status.class.php';//引入<span style="color: rgb(65, 105, 225);"><font size="5"> </font><font size="3">status.class.php</font></span>
        
    $status = new MinecraftServerStatus(); // call the class
    $response = $status-> getStatus('<font color="#4169e1">服务器IP</font>', '<font color="#4169e1">服务器端口</font>');
if(!$response) {
    echo"The Server is offline!";
} else {
    echo"<img width="64" height="64" src="asas1.png".$response['favicon']."" /> <br>
    服务器信息 ".'</br>'.'服务器IP'.$response['hostname']." 在线状态: ".$response['version']." 目前在线人数:".$response['players']."位玩家 最大在线人数: ".$response['maxplayers']."人".". 服务器版本:'".$response['motd']."'.
    延迟:".$response['ping']." 毫秒.";
}
?>
</body>
</html>
回复

使用道具 举报

men62466293 当前离线
积分
1562
帖子
主题
精华
贡献
爱心
钻石
人气
下界之星
最后登录
1970-1-1
注册时间
2018-6-12
查看详细资料
头像被屏蔽

来自:河北

12#
 楼主| 发表于 2020-5-15 21:50:02 | 只看该作者

Parse error: syntax error, unexpected '64' (T_LNUMBER), expecting ',' or ';' in G:\Desktop\test\index.php on line 14
回复

使用道具 举报

strings 当前离线
积分
26267
帖子
主题
精华
贡献
爱心
钻石
人气
下界之星
最后登录
1970-1-1
注册时间
2020-4-29
查看详细资料

来自:湖南

13#
发表于 2020-5-15 21:51:57 | 只看该作者
men62466293 发表于 2020-5-15 21:50
Parse error: syntax error, unexpected '64' (T_LNUMBER), expecting ',' or ';' in G:\Desktop\test\in ...

论坛把转义吞掉了
<html>
<head>
<title>Minecraft server</title>
</head>
<body>
<?php
        include_once 'status.class.php';//引入<span style="color: rgb(65, 105, 225);"><font size="5"> </font><font size="3">status.class.php</font></span>
        
    $status = new MinecraftServerStatus(); // call the class
    $response = $status-> getStatus('<font color="#4169e1">服务器IP</font>', <font color="#4169e1">服务器端口</font>);

if(!$response) {
    echo"The Server is offline!";
} else {
    echo"<img width=\"64\" height=\"64\" src=\"asas1.png".$response['favicon']."\" /> <br>
    服务器信息 ".'</br>'.'服务器IP'.$response['hostname']." 在线状态: ".$response['version']." 目前在线人数:".$response['players']."位玩家 最大在线人数: ".$response['maxplayers']."人".". 服务器版本:'".$response['motd']."'.
    延迟:".$response['ping']." 毫秒.";
}

?>
</body>
</html>

<?php
    class MinecraftServerStatus {

        private $timeout;

        public function __construct($timeout = 2) {
            $this->timeout = $timeout;
        }

        public function getStatus($host = '127.0.0.1', $version = '1.7.*' , $port = 25565) {

            if (substr_count($host , '.') != 4) $host = gethostbyname($host);

            $serverdata = array();
            $serverdata['hostname'] = $host;
            $serverdata['version'] = false;
            $serverdata['protocol'] = false;
            $serverdata['players'] = false;
            $serverdata['maxplayers'] = false;
            $serverdata['motd'] = false;
            $serverdata['motd_raw'] = false;
            $serverdata['favicon'] = false;
            $serverdata['ping'] = false;

            $socket = $this->connect($host, $port);

            if(!$socket) {
                return false;
            }

            if(preg_match('/1.7|1.8/',$version)) {

                $start = microtime(true);

                $handshake = pack('cccca*', hexdec(strlen($host)), 0, 0x04, strlen($host), $host).pack('nc', $port, 0x01);

                socket_send($socket, $handshake, strlen($handshake), 0); //give the server a high five
                socket_send($socket, "\x01\x00", 2, 0);
                socket_read( $socket, 1 );

                $ping = round((microtime(true)-$start)*1000); //calculate the high five duration

                $packetlength = $this->read_packet_length($socket);

                if($packetlength < 10) {
                    return false;
                }

                socket_read($socket, 1);

                $packetlength = $this->read_packet_length($socket);

                $data = socket_read($socket, $packetlength, PHP_NORMAL_READ);

                if(!$data) {
                    return false;
                }

                $data = json_decode($data);

                $serverdata['version'] = $data->version->name;
                $serverdata['protocol'] = $data->version->protocol;
                $serverdata['players'] = $data->players->online;
                $serverdata['maxplayers'] = $data->players->max;

                $motd = $data->description;
                $motd = preg_replace("/(§.)/", "",$motd);
                $motd = preg_replace("/[^[:alnum:][:punct:] ]/", "", $motd);

                $serverdata['motd'] = $motd;
                $serverdata['motd_raw'] = $data->description;
                $serverdata['favicon'] = $data->favicon;
                $serverdata['ping'] = $ping;

            } else {

                $start = microtime(true);

                socket_send($socket, "\xFE\x01", 2, 0);
                $length = socket_recv($socket, $data, 512, 0);

                $ping = round((microtime(true)-$start)*1000);//calculate the high five duration
         
                if($length < 4 || $data[0] != "\xFF") {
                    return false;
                }

                $motd = "";
                $motdraw = "";

                //Evaluate the received data
                if (substr((String)$data, 3, 5) == "\x00\xa7\x00\x31\x00"){

                    $result = explode("\x00", mb_convert_encoding(substr((String)$data, 15), 'UTF-8', 'UCS-2'));
                    $motd = $result[1];
                    $motdraw = $motd;

                } else {

                    $result = explode('§', mb_convert_encoding(substr((String)$data, 3), 'UTF-8', 'UCS-2'));
                        foreach ($result as $key => $string) {
                            if($key != sizeof($result)-1 && $key != sizeof($result)-2 && $key != 0) {
                                $motd .= '§'.$string;
                            }
                        }
                        $motdraw = $motd;
                    }

                    $motd = preg_replace("/(§.)/", "", $motd);
                    $motd = preg_replace("/[^[:alnum:][:punct:] ]/", "", $motd); //Remove all special characters from a string

                    $serverdata['version'] = $result[0];
                    $serverdata['players'] = $result[sizeof($result)-2];
                    $serverdata['maxplayers'] = $result[sizeof($result)-1];
                    $serverdata['motd'] = $motd;
                    $serverdata['motd_raw'] = $motdraw;
                    $serverdata['ping'] = $ping;

            }

            $this->disconnect($socket);

            return $serverdata;

        }

        private function connect($host, $port) {
            $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
            socket_connect($socket, $host, $port);
            return $socket;
        }

        private function disconnect($socket) {
            if($socket != null) {
                socket_close($socket);
            }
        }

        private function read_packet_length($socket) {
            $a = 0;
            $b = 0;
            while(true) {
                $c = socket_read($socket, 1);
                if(!$c) {
                    return 0;
                }
                $c = Ord($c);
                $a |= ($c & 0x7F) << $b++ * 7;
                if( $b > 5 ) {
                    return false;
                }
                if(($c & 0x80) != 128) {
                    break;
                }
            }
            return $a;
        }

    }
?>
回复

使用道具 举报

men62466293 当前离线
积分
1562
帖子
主题
精华
贡献
爱心
钻石
人气
下界之星
最后登录
1970-1-1
注册时间
2018-6-12
查看详细资料
头像被屏蔽

来自:河北

14#
 楼主| 发表于 2020-5-15 21:54:22 | 只看该作者

Fatal error: Cannot declare class MinecraftServerStatus, because the name is already in use in G:\Desktop\test\status.class.php on line 2
回复

使用道具 举报

strings 当前离线
积分
26267
帖子
主题
精华
贡献
爱心
钻石
人气
下界之星
最后登录
1970-1-1
注册时间
2020-4-29
查看详细资料

来自:湖南

15#
发表于 2020-5-15 21:56:25 | 只看该作者
men62466293 发表于 2020-5-15 21:54
Fatal error: Cannot declare class MinecraftServerStatus, because the name is already in use in G:\ ...

<?php
    class MinecraftServerStatus {

起是第二个文件
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册(register)

本版积分规则

Archiver|小黑屋|Mcbbs.net ( 京ICP备15023768号-1 ) | 京公网安备 11010502037624号 | 手机版

GMT+8, 2023-12-30 17:14 , Processed in 0.053320 second(s), Total 31, Slave 30 queries, Release: Build.2023.11.27 0934, Gzip On, Redis On.

"Minecraft"以及"我的世界"为美国微软公司的商标 本站与微软公司没有从属关系

© 2010-2023 我的世界中文论坛 版权所有 本站内原创内容版权属于其原创作者,除作者或版规特别声明外未经许可不得转载