Jump to content

PHP doesn't work on include


Asmo
Go to solution Solved by DavidAM,

Recommended Posts

Hey there, I've made a php script to load the status of one of my gameservers with fsockopen. However, to avoid pinging over and over again i used a "cached" file (status.txt) in order to get the status from there if 30 sec didn't pass from last pinging.

 

Everything works well, but I want to have it as a seperated file and include it on index.php via <?php include("status.php") ?> 

When I open the file straight from my browser it works, but when I include it on my index it doesnt. What could be wrong.

 

<?php
ini_set( "display_errors", 1); //hide fsockopen/fopen warnings if file doesn't exist or couldn't connect
$flogin = 0;
$fgame = 0;


$g_Ip = "178.175.141.27"; //Server ip
$g_Port = "7777"; //Server Port


$l_Ip = "178.175.141.27"; //Server ip
$l_Port = "2106"; //Server Port


function IsOnline($ip, $port)
{
$sock=@fsockopen($ip, $port, $errNo, $errStr, 3);//timeout set to 3 seconds
if($sock)
{
fclose($sock);
return 1;
}
return 0;
}


function RefreshStatus()
{
global $g_Ip, $g_Port, $l_Ip, $l_Port;
$status = IsOnline($g_Ip, $g_Port);
$status2 = IsOnline($l_Ip, $l_Port);
//storing info about timestamp and server status
$file = fopen("status.txt", "wb");
$timestamp = time() + 30; //it will refresh every 30 seconds - won't flood the server
$cont = $timestamp .' '. $status .' '. $status2;
fwrite($file, $cont);
fclose($file);
return $status2;
}


$file = fopen("status.txt", "r");
if(!$file)
{
//file doesn't exist
RefreshStatus();
$fgame = $data[1];
$flogin = $data[2];
}else
{
$cont = fread($file, filesize("status.txt"));
$data = explode(" ", $cont);
if($data[0] < time())
{
//refresh status
RefreshStatus();
$fgame = $data[1];
$flogin = $data[2];
}else
{
$fgame = $data[1];
$flogin = $data[2];
}
}




if ($flogin)  
{ echo'<table width="100%" cellspacing="0" cellpadding="2">';
$bg1=BG_1;
$bg2=BG_2;
 echo '<li style="background-color:'.$bg1.'">
 <strong>Loginserver:</strong>
     <span class="result"><font color="'.$wincolor.'">ONLINE</font></span>
</li>';


}  
else  


{ echo'<table width="100%" cellspacing="0" cellpadding="2">';
$bg1=BG_1;
$bg2=BG_2;
echo '<li style="background-color:'.$bg1.'">
 <strong>Loginserver</strong>
     <span class="result"><font color="'.$loosecolor.'">OFFLINE</font></span>
</li>';


} 


if ($fgame)  
{ 
$bg1=BG_3;
$bg2=BG_4;
echo '<li style="background-color:'.$bg1.'">
 <strong>Gameserver:</strong>
     <span class="result"><font color="'.$wincolor.'">ONLINE</font></span>
</li>';
echo'</table>';
}  
else  


{
$bg1=BG_3;
$bg2=BG_4;
echo '<li style="background-color:'.$bg1.'">
 <strong>Gameserver:</strong>
     <span class="result"><font color="'.$loosecolor.'">OFFLINE</font></span>
</li>';
echo'</table>';
}


?>

Best regards.

 

Link to comment
Share on other sites

It's in the same folder. I'm not that dumb :P

 

If I use this one there's no problem, but it pings the server on every view.

 

<?php 
ini_set( "display_errors", 1); //hide fsockopen/fopen warnings if file doesn't exist or couldn't connect
$flogin = @fsockopen ("178.175.141.27",2106, $errno, $errstr, 1); 
$fgame = @fsockopen ("178.175.141.27",7777, $errno, $errstr, 1); 




if ($flogin)  
{ echo'<table width="100%" cellspacing="0" cellpadding="2">';
$bg1=BG_1;
$bg2=BG_2;
 echo '<li style="background-color:'.$bg1.'">
 <strong>Loginserver:</strong>
     <span class="result"><font color="'.$wincolor.'">ONLINE</font></span>
</li>';


}  
else  


{ echo'<table width="100%" cellspacing="0" cellpadding="2">';
$bg1=BG_1;
$bg2=BG_2;
echo '<li style="background-color:'.$bg1.'">
 <strong>Gameserver:</strong>
     <span class="result"><font color="'.$loosecolor.'">OFFLINE</font></span>
</li>';


} 


if ($fgame)  
{ 
$bg1=BG_3;
$bg2=BG_4;
echo '<li style="background-color:'.$bg1.'">
 <strong>Gameserver:</strong>
     <span class="result"><font color="'.$wincolor.'">ONLINE</font></span>
</li>';
echo'</table>';
}  
else  


{
$bg1=BG_3;
$bg2=BG_4;
echo '<li style="background-color:'.$bg1.'">
 <strong>Gameserver:</strong>
     <span class="result"><font color="'.$loosecolor.'">OFFLINE</font></span>
</li>';
echo'</table>';
}


?>
Edited by Asmo
Link to comment
Share on other sites

It'd help if you actually defined what happens rather than just say it doesn't work. Do you get any errors? Does anything show at all or just a blank page? If the status.txt file doesn't exist, does it get created?

 

If you want help, you need to provide details, not just say it doesn't work.

Edited by kicken
Link to comment
Share on other sites

  • Solution

You are referencing $data if the file does not exist so $data has not been defined. You are using globals which is a real no-no. You have repeated code. And you are processing a file at times you don't need to.

 

Could we get this to "work"? Yes. But if you ever need to add a server (for scores or mirrors), you will have to rewrite the entire thing.

 

Try this:

<?php
ini_set( "display_errors", 1); //hide fsockopen/fopen warnings if file doesn't exist or couldn't connect

define('C_STATUS_FILE', 'status.txt');	// Status File Path
define('C_STATUS_AGE', 	30);			// Seconds to cache server status

// List of interesting servers 
$servers = array('Game' => array('IP' => "178.175.141.27", 'Port' => "7777", 'Status' => 0), 
				'Login' => array('IP' => "178.175.141.27", 'Port' => "2106", 'Status' => 0));

/* Check to see if a particular server is online 
		Returns (int) 1 for YES and 0 (zero) for NO 	*/
function IsOnline($paServer) {
	$sock=@fsockopen($paServer['IP'], $paServer['Port'], $errNo, $errStr, 3);//timeout set to 3 seconds
	if($sock) {
		fclose($sock);
		return 1;
	}
	return 0;
}

/* Get the status of all servers
		The server array is passed by reference and is UPDATED
		
		If the status cache file exists and is less than C_STATUS_AGE seconds old, we read it;
			otherwise we try to connect to each server and update the status cache file. 	*/
function RefreshStatus(&$paServers) {
	if ( (file_exists(C_STATUS_FILE)) and (filemtime(C_STATUS_FILE) < time() - C_STATUS_AGE) ) {
		$paServers = unserialize(file_get_contents(C_STATUS_FILE));
		return;
	}

	foreach ($paServers as $name => $data) {
		$paServers[$name]['Status'] = IsOnline($data);
	}
	
	file_put_contents(C_STATUS_FILE, serialize($paServers));
}

// Get the server statuses and output the table
RefreshStatus($servers);

echo'<table width="100%" cellspacing="0" cellpadding="2">';
foreach ($servers as $name => $data) {
	if ($data['Status']) {
		$color = $wincolor;
		$status = 'ONLINE';
	} else {
		$color = $loosecolor;
		$status = 'OFFLINE';
	}
	echo '<li style="background-color:' . BG_1 . '">
 <strong>' . $name . ' Server:</strong>
     <span class="result"><font color="' . $color . '">' . $status . '</font></span>
</li>';

}
echo'</table>';
I changed the status file format, so delete it before your first run. The code uses an array to hold the server data, so you can add other servers (later) in there if necessary without changing any of the other code.

 

The RefreshStatus() function uses the file-system timestamp rather than a value in the file. This way we don't waste resources opening and reading a file we don't need. The data is stored as a serialized array ($servers) so we can just read it and unserialize instead of trying to parse it. There should probably be a check that if the unserialize fails (because the file is corrupt), it will goahead and check the servers and write a new file.

 

I stripped down your output. There was no real difference between the two servers, so we put it in a loop. Now, if you want to change the output format, you do it only ONCE and all servers are done. Note: the first thing you should do is get rid of the (deprecated) FONT tags.

 

I have not tested this, so there might be some typos. But read through it, and try to understand it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.