PHP Tutorial: Minecraft Server Information (No Plugins Neede
Posted: Mon May 06, 2013 7:36 pm
I am getting a fsocket error that says that the connection timed out, but the server is clearly on im in it right now. Please help
The non-existent community
https://betterphp.co.uk/board/
<?php function fetch_server_info($ip, $port){ $socket = fsockopen($ip, $port, $errno, $errstr, 0.5); if($socket === false){ return false; } fwrite($socket, "\xfe"); $data = fread($socket, 256); if(substr($data, 0, 1) != "\xff"){ return false; } $data = explode('§', mb_convert_encoding(substr($data, 3), 'UTF8', 'UCS-2')); return array( 'motd' => $data[0], 'players' => intval($data[1]), 'max_players' => intval($data[2]), ); } ?>this is the index page code
<?PHP include('PHP/core/init.inc.php'); foreach ($config['servers'] as $server){ $server_ip = $server[0]; $server_port = $server[1]; } $info = fetch_server_info($server_ip, $server_port); ?> <div id="box4"> <?PHP echo '<h3>', $server_ip, '</h3>'; if($info === false){ echo '<p><font color = "#FFF000">Status:</font> <font color = "#FF0000">Offline</font></P>'; echo '<font color = "#FFF000">Note: If this says ofline but your positive the server is online check your fire wall or internet connection</font>'; }else{ echo '<p><font color = "#FFF000">Status:</font> <font color = "#00FF00">Online</font></p>'; echo '<p><font color = "#FFF000">Motd: ', $info['motd'], '</font></p>'; echo '<p><font color = "#FFF000">Players:', $info['players'], ' / ', $info['max_players'], '</font></p>'; } ?> </div>
That didnt, work if you look at the code above im already doing that in the php code.Helx wrote:In your <head> try putting this: <meta charset="utf-8">
See if that helps
Nope didn't work, that shouldn't have mattered at all since UTF8 and utf-8 are the same thing, in minecraft my friend has color codes around his motd which look like this §a, well since im already exploding the §, im thinking the php is still trying to say ok what in UTF8 will match the color codes that minecraft is sending and is replacing them with  and what ever number is ment to be with it for the color.Helx wrote:Line 18 of mc.inc.php, change UTF8 to utf-8
error_reporting(E_ALL); ini_set('display_errors', '1');At the top of your code and see if anything comes up.
$example = array( [0]=> "Â" [1]=> "4MineÂ" [2]=> "bMad Â" [3]=> "4Multi-World Â" [4]=> "f[1.5.1]Â" [5]=> "1Â" [6]=> "15" ); $pattern = array('\Â\', '\4\'); implode(' ', preg_replace($pattern, '', array_slice($example, 0, -2)));That would *probably* take care of the title. You can apply the same method to the player count just changed the offset of array_slice.
I'm still getting the triangles.array(3) { [0]=> string(20) "Minecraft Adventure�" [1]=> string(2) "0�" [2]=> string(2) "20" }
<?php function fetch_server_info($ip, $port){ $socket = @fsockopen($ip, $port, $errno, $errstr, 0.5); if($socket === false){ return false; } fwrite($socket, "\xFE"); $data = fread($socket, 256); //var_dump($data); if(substr($data, 0, 1) != "\xFF"){ return false; } $data = explode('§', mb_convert_encoding(substr($data, 3), 'UTF8', 'UCS-2')); var_dump($data); return array( 'motd' => $data[0], 'players' => intval($data[1]), 'max_players' => intval($data[2]), ); } ?>server_list.php
<?php include('core/init.inc.php'); ?> <!DOCTYPE html public "-//W3C//DTD XHTML 1.1 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Minecraft server list</title> </head> <body> <div> <?php foreach($config['servers'] as $id => $server){ echo '<p><a href="view_server.php?sid=', $id, '">', $server[0],'</a></p>'; } ?> </div> </body> </html>view_server.php
<?php include('core/init.inc.php'); if (!isset($_GET['sid']) || empty($config['servers'][$_GET['sid']])){ header('location: server_list.php'); die(); } $server_ip = $config['servers'][$_GET['sid']][0]; $server_port = $config['servers'][$_GET['sid']][1]; $info = fetch_server_info($server_ip, $server_port); ?> <!DOCTYPE html public "-//W3C//DTD XHTML 1.1 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title><?php echo $server_ip; ?> Status</title> </head> <body> <div> <h1><?php echo $server_ip; ?></h1> <?php if($info === false){ echo '<p>Status: <span style="color:red">Offline</span></p>'; }else{ echo '<p>Status: <span style="color:green">Online</span></p>'; echo '<p>Motd: ', $info['motd'], '</p>'; echo '<p>Players: ', $info['players'],' / ',$info['max_players'], '</p>'; } ?> </div> <a href="server_list.php">Back</a> </body> </html>config.inc.php
<?php $config['servers'][] = array('bearlyconnected.com', 25565); $config['servers'][] = array('bearlyconnected.com', 25566); $config['servers'][] = array('bearlyconnected.com', 25567); $config['servers'][] = array('server.mcbans.com', 25565); ?>Please help with this issue if possible.