Asmo Posted October 15, 2013 Share Posted October 15, 2013 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. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 15, 2013 Share Posted October 15, 2013 make sure status.php is in the same folder as index.php. What url are you using to view status.php and what url are using to view index.php? Quote Link to comment Share on other sites More sharing options...
Asmo Posted October 15, 2013 Author Share Posted October 15, 2013 (edited) It's in the same folder. I'm not that dumb 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 October 15, 2013 by Asmo Quote Link to comment Share on other sites More sharing options...
Asmo Posted October 15, 2013 Author Share Posted October 15, 2013 None would help with this? Quote Link to comment Share on other sites More sharing options...
kicken Posted October 15, 2013 Share Posted October 15, 2013 (edited) 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 October 15, 2013 by kicken Quote Link to comment Share on other sites More sharing options...
.josh Posted October 15, 2013 Share Posted October 15, 2013 you aren't assigning a value to $data unless the file already exists. IOW $data doesn't exist unless the file exists, but you try to reference it when the file doesn't exist. Quote Link to comment Share on other sites More sharing options...
Asmo Posted October 16, 2013 Author Share Posted October 16, 2013 So, yeap. The file is being created when i open the index.php where i include status.php. However it doesn't show anything. No errors, nothing. If i access status php directly, it works like a charm. Quote Link to comment Share on other sites More sharing options...
.josh Posted October 16, 2013 Share Posted October 16, 2013 Right, but in this condition block: if(!$file) { //file doesn't exist RefreshStatus(); $fgame = $data[1]; $flogin = $data[2]; }else Yes, you create the file, but this code block doesn't assign anything to $data so you're trying to reference $data[1] and $data[2] when they don't exist. Quote Link to comment Share on other sites More sharing options...
Asmo Posted October 16, 2013 Author Share Posted October 16, 2013 You're right, but even tho it doesn't change anything.. as you can see it works if you load directly the php file from the browser. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted October 16, 2013 Share Posted October 16, 2013 You're doing it wrong man! Are you sure that the script with a basic file reach this include function? Can you show us both files, please? Quote Link to comment Share on other sites More sharing options...
Asmo Posted October 16, 2013 Author Share Posted October 16, 2013 I'm pretty sure I'm doing it right. If i replace the code with the code i pasted on reply #3 it works like a charm. There must be something to do with the variables or dunno why it doesn't echoing.. Quote Link to comment Share on other sites More sharing options...
Solution DavidAM Posted October 16, 2013 Solution Share Posted October 16, 2013 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. Quote Link to comment Share on other sites More sharing options...
Asmo Posted October 17, 2013 Author Share Posted October 17, 2013 Thank you everyone, actually I'm more than dumb. I was using a function named IsOnline to check if user is currently logged it. I guess there was an issue with that. Sorry and thank you again! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.