PHP Tutorial: Minecraft Server Information (No Plugins)

Post here is you are having problems with any of the tutorials.
Post Reply
SicX
Posts: 26
Joined: Fri Jun 15, 2012 1:03 pm

PHP Tutorial: Minecraft Server Information (No Plugins)

Post by SicX »

Hey guys,
I'm from germany and I've tried to follow your tut, but if the server is offline it still shows:
Status: Online
Slots: f / f

Any idea what kind of mistake i made? heres the code:
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: Offline<br>', 'Players: - / -</p>';
				
				}else{
					
					echo '<p>Status: Online<br>', 'Players: ', $info['players'], ' / ', $info['max_players'], '</p>';
					}
				
				?>
			
			
		</div>
	</body>
</html>
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>Hallo</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>
init.inc.php:
<?php

$path = dirname(__FILE__);

include("{$path}/config.inc.php");
include("{$path}/inc/mc.inc.php");

?>
config.inc.php:
<?php

$config['servers'][] = array('mc.somethingcraft.co.uk', 25565);
$config['servers'][] = array('test123', 25565);     #To test an offline server!

?>
mc.inc.php:
<?php

//fetches server information.
function fetch_server_info($ip, $port){
	$socket = @fsockopen($ip, $port, $errno, $errstr, 0.5);

	if ($socket === false){
		return flase;
	}
	
	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]),
	);
}

?>
Changed the output a little bit, just for looks (I don't need the motd - hope it doesn't matter).

The folders:
status:
- view_server.php
- server_list.php
---> core
---- config.inc.php
---- init.inc.php
-----> inc
------ mc.inc.php

Greetings from germany,
SicX!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP Tutorial: Minecraft Server Information (No Plugins)

Post by jacek »

if ($socket === false){
                return flase;
        }
should be
if ($socket === false){
                return false;
        }
The reason you were getting the "f" values is because php converts the invalid constant to a string "flase", your other code then took the first character of this.

:)
Image
SicX
Posts: 26
Joined: Fri Jun 15, 2012 1:03 pm

Re: PHP Tutorial: Minecraft Server Information (No Plugins)

Post by SicX »

Ah!
Thank you!! :)
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP Tutorial: Minecraft Server Information (No Plugins)

Post by jacek »

:D No problem :D
Image
Post Reply