PHP Tutorial: Minecraft Server Information (No Plugins Neede

Post here is you are having problems with any of the tutorials.
Post Reply
SureShotM
Posts: 14
Joined: Mon May 06, 2013 3:44 pm

PHP Tutorial: Minecraft Server Information (No Plugins Neede

Post by SureShotM »

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
User avatar
FrederickGeek8
Posts: 148
Joined: Wed Nov 30, 2011 10:31 pm

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

Post by FrederickGeek8 »

Post the code that you have. We won't be able to help you without it.
SureShotM
Posts: 14
Joined: Mon May 06, 2013 3:44 pm

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

Post by SureShotM »

Actualy I just figured out that its not my code but my host, I either need to move to a new host or upgrade mine because right now the port is locked on there side and wont unlock it unless I uprgade
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

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

Post by Helx »

Most VPS services have all ports open.
SureShotM
Posts: 14
Joined: Mon May 06, 2013 3:44 pm

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

Post by SureShotM »

Ok well im actualy using 00webhost since it was free and im using it for testing reasons, and when i sent them a ticket asking about the port they told me they have it closed. Now im not sure if there just saying that to get me to buy, so here is my code.

this is the mc.inc.php
<?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>
SureShotM
Posts: 14
Joined: Mon May 06, 2013 3:44 pm

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

Post by SureShotM »

also what vps hos would you suggest.
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

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

Post by Helx »

I would suggest x10vps.com (link).
If you have any problems with their service, you can always submit a ticket.

There customer service is excellent :)
SureShotM
Posts: 14
Joined: Mon May 06, 2013 3:44 pm

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

Post by SureShotM »

Ok ill give it a try thanks for the help.
SureShotM
Posts: 14
Joined: Mon May 06, 2013 3:44 pm

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

Post by SureShotM »

ok so i got the server and the php code sorta works, it connects to it, but the motd doesnt show, it shows A with a squigly line above it , and then the players go either 0/0 or 4/0
SureShotM
Posts: 14
Joined: Mon May 06, 2013 3:44 pm

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

Post by SureShotM »

ok so i figured out something, its actualy a color code this is what i am getting array(7) { [0]=> string(1) "Â" [1]=> string(6) "4MineÂ" [2]=> string(6) "bMad Â" [3]=> string(14) "4Multi-World Â" [4]=> string(9) "f[1.5.1]Â" [5]=> string(2) "1Â" [6]=> string(2) "15" } how can i clean it up in my code so it looks like MineMad Multi-World [1.5.1] then for the players to be 1/15 with out the  after the 1
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

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

Post by Helx »

In your <head> try putting this: <meta charset="utf-8">
See if that helps :)
SureShotM
Posts: 14
Joined: Mon May 06, 2013 3:44 pm

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

Post by SureShotM »

Helx wrote:In your <head> try putting this: <meta charset="utf-8">
See if that helps :)
That didnt, work if you look at the code above im already doing that in the php code.
SureShotM
Posts: 14
Joined: Mon May 06, 2013 3:44 pm

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

Post by SureShotM »

any other idea please, :?
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

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

Post by Helx »

Line 18 of mc.inc.php, change UTF8 to utf-8
SureShotM
Posts: 14
Joined: Mon May 06, 2013 3:44 pm

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

Post by SureShotM »

Helx wrote:Line 18 of mc.inc.php, change UTF8 to utf-8
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.
SureShotM
Posts: 14
Joined: Mon May 06, 2013 3:44 pm

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

Post by SureShotM »

if there are any more ideas i would loe to here them, cause this is pissing me off. :x
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

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

Post by Helx »

I really have no clue :/
All I can suggest is that you put
error_reporting(E_ALL);
ini_set('display_errors', '1');
At the top of your code and see if anything comes up.
SureShotM
Posts: 14
Joined: Mon May 06, 2013 3:44 pm

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

Post by SureShotM »

its not error though, im not getting an error, I found help some were else thanks for trying though.
User avatar
FrederickGeek8
Posts: 148
Joined: Wed Nov 30, 2011 10:31 pm

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

Post by FrederickGeek8 »

I don't know where the error is coming from but you could just preg_replace everything in the arrays.
$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.
jarrodatt
Posts: 13
Joined: Wed Sep 26, 2012 12:21 am

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

Post by jarrodatt »

Hello,
I'm still having some issues with utf8 stuff.
array(3) { [0]=> string(20) "Minecraft Adventure�" [1]=> string(2) "0�" [2]=> string(2) "20" }
I'm still getting the triangles.

here's my mc.inc.php code:
<?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.
Post Reply