FragLand / minestat

:chart_with_upwards_trend: A Minecraft server status checker
GNU General Public License v3.0
354 stars 71 forks source link

PhP can't load 500 #29

Closed Pomdre closed 5 years ago

Pomdre commented 5 years ago

HTTP ERROR 500 on php. Hope you can fix it :)

sorbanbela commented 5 years ago

I think it will be not enough to help him to find&fix the issue. You probably also attach some information about when this is happening, how do you wrote your code where you're using his work. Also would be great if you check your apache webserver log files.

You can check where is your error log file with the phpinfo code: <?php phpinfo(); ?>

Pomdre commented 5 years ago

This happens when I load the example file.

Here is the code I use. It's the same as he has...

Don't know if it's needed but here is my phpinfo: https://www.pomdre.net/info.php

<?php require_once('./minestat.php'); $ms = new MineStat("minecraft.dilley.me", 25565); printf("Minecraft server status of %s on port %s:<br>", $ms->get_address(), $ms->get_port()); if($ms->is_online()) { printf("Server is online running version %s with %s out of %s players.<br>", $ms->get_version(), $ms->get_current_players(), $ms->get_max_players()); printf("Message of the day: %s<br>", $ms->get_motd()); printf("Latency: %sms<br>", $ms->get_latency()); } else { printf("Server is offline!<br>"); } ?>

<?php class MineStat { const DATA_SIZE = 512; // this will hopefully suffice since the MotD should be <=59 characters const NUM_FIELDS = 6; // number of values expected from server private $address; private $port; private $online; // online or offline? private $version; // Minecraft server version private $motd; // message of the day private $current_players; // current number of players online private $max_players; // maximum player capacity private $latency; // ping time to server in milliseconds public function __construct($address, $port, $timeout = 5) { $this->address = $address; $this->port = $port; try { $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0)); if($socket === false) { $this->online = false; return; } $start_time = microtime(); $result = socket_connect($socket, $address, $port); $this->latency = round((microtime() - $start_time) * 1000); if($result === false) { $this->online = false; return; } $payload = "\xFE\x01"; socket_write($socket, $payload, strlen($payload)); $raw_data = socket_read($socket, MineStat::DATA_SIZE); socket_close($socket); } catch(Exeption $e) { $this->online = false; return; } if(isset($raw_data)) { $server_info = explode("\x00\x00\x00", $raw_data); if(isset($server_info) && sizeof($server_info) >= MineStat::NUM_FIELDS) { $this->online = true; $this->version = $server_info[2]; $this->motd = $server_info[3]; $this->current_players = $server_info[4]; $this->max_players = $server_info[5]; } else $this->online = false; } else $this->online = false; } public function get_address() { return $this->address; } public function get_port() { return $this->port; } public function is_online() { return $this->online; } public function get_version() { return $this->version; } public function get_motd() { return $this->motd; } public function get_current_players() { return $this->current_players; } public function get_max_players() { return $this->max_players; } public function get_latency() { return $this->latency; } } ?>

ldilley commented 5 years ago

@Raideerke is correct. Additionally, MineStat does not speak HTTP nor do the vanilla Minecraft, Spigot, or Bungee JARs. As a consequence, the HTTP error code 500 is not coming from any of these programs. This issue stems from the web server software you are using and the PHP interpreter potentially choking on some code.

The PHP implementation of MineStat from the master branch (fff42ec) works fine here using version 7.2.15 of the PHP CLI interpreter on Fedora 29. I typically test each implementation prior to making a commit. It looks like you're even using a more recent version which should be fine.

I've reviewed the phpinfo() output (which I see no problems with). So, you can remove the info.php file on your end now if you would like to keep your server more secure. I'd suggest checking your web server error logs to see if they provide any further details. You can also temporarily enable verbose error/warning output for more visibility by doing the following:

Performing the above should help track down the issue.

Pomdre commented 5 years ago

Okay thanks. Have this error now :D

Fatal error: Uncaught Error: Call to undefined function socket_create() in /customers/6/0/c/pomdre.net/httpd.www/game-api/minestat.php:20 Stack trace: #0 /customers/6/0/c/pomdre.net/httpd.www/game-api/index.php(7): MineStat->__construct('minecraft.dille...', 25565) #1 {main} thrown in /customers/6/0/c/pomdre.net/httpd.www/game-api/minestat.php on line 20
Pomdre commented 5 years ago

This is strange line 20 looks really nice....

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

ldilley commented 5 years ago

Just a small note/tip, @Pomdre: GitHub supports code blocks with syntax highlighting. See how the PHP code you pasted in your 2nd message appears to be all jumbled together making it difficult to read? If you edit your response, you can wrap the PHP code in a block similar to the following example to make it appear nicely:

```php
// comment
<?php
phpinfo();
?>
``` (end with 3 backticks to close)

You can change the language at the end of the opening backticks to modify the way keywords, operators, literals, etc. are colored. Some popular languages that are supported: c, cpp, cs or csharp, go, java, php, python, ruby, xml, yaml, and more. I hope this helps. :)

ldilley commented 5 years ago

Per the PHP manual, socket_create() is certainly supported in PHP 7 with the constants we've passed to the function: http://php.net/manual/en/function.socket-create.php

It seems like you may not have the sockets extension loaded? We may have missed that in your PHP info output...

Pomdre commented 5 years ago

Okay https://www.pomdre.net/info.php

ldilley commented 5 years ago

Have a look here: http://tufat.com/docs/flashchat/socket_server_linux.html

Also try pulling the following up in a browser to see if it does indeed return true:

if (!extension_loaded('sockets')) {
    die('The sockets extension is not loaded.');
}
Pomdre commented 5 years ago

okay doesn't look like the socket module is installed.

ldilley commented 5 years ago

Excellent; so we've determined the problem. :)

This is not directly a MineStat issue, but I do not mind assisting. The installation section of http://php.net/manual/en/book.sockets.php states that the sockets extension must be enabled at compile time: http://php.net/manual/en/sockets.installation.php

I am not sure if you compiled PHP from source? The packages provided by RHEL/CentOS and Fedora already have prebuilt PHP binaries with sockets support. I am not certain what platform you are using. If compiling from source, you'll need to rebuild (and set your prefix during the configure phase using --prefix=/path if desired):

Pomdre commented 5 years ago

And there's my problem. Those I host(one.com) at will not install socket :( some way around?

ldilley commented 5 years ago

Unfortunately, that is a limitation of some of the shared web hosting providers. I'd recommend a low-cost VPS if you are comfortable maintaining a Linux system. Here are some popular VPS providers who have datacenters in Asia, Australia, Europe, and North America along with their pricing (full root access for as little as $2.50/month):

Or if you prefer to continue using shared web hosting, A2 Hosting is a reputable provider that has been around for years and supports PHP sockets: https://www.a2hosting.com/

ldilley commented 5 years ago

I'll go ahead and close this issue since you now have a way forward and have not replied in over a week, @Pomdre.