Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a webpage that shows the amount of online players on a game server in real time.

The problem is that I can get the amount of players online in the game server displayed in the website, but after loading the page it never updates and it always stays as the initial amount of players, although lots of people join and leave the server every second.

This is the PHP code that I am using to show the numbers:

<?php
	echo "<a id='a1' href='#' class='online'>Loading...</a>";
?>


What I am doing is to update 'a1' every second with the new amount of online players using javascript, which updates it with a php function called 'getplayers()':

XML
<script language="JavaScript">
setInterval(function(){
    document.getElementById("a1").innerHTML = '<?php echo getplayers()?>';
}, 1000);
</script>



The function 'getplayers()' is like:

<?php
include "Status.php";
function getplayers() {
	$serverb = new Status("127.0.0.1", '17171');
	return $serverb->online_players;
}
?>



Lastly, Status.php is a php script that gets the amount of players online and more things about the server, which I am sure that works:

PHP
<html>
<?php

class Status {

    public $server;
    public $online, $motd, $online_players, $max_players;
    public $error = "OK";
    function __construct($url, $port = '25565') {
        $this->server = array(
            "url" => $url,
            "port" => $port
        );
        if ( $sock = @stream_socket_client('tcp://'.$url.':'.$port, $errno, $errstr, 1) ) {
            $this->online = true;
            fwrite($sock, "\xfe");
            $h = fread($sock, 2048);
            $h = str_replace("\x00", '', $h);
            $h = substr($h, 2);
            $data = explode("\xa7", $h);
            unset($h);
            fclose($sock);
            if (sizeof($data) == 3) {
                $this->motd = $data[0];
                $this->online_players = (int) $data[1];
                $this->max_players = (int) $data[2];
            }
            else {
                $this->error = "Cannot retrieve server info.";
            }
        }
        else {
            $this->online = false;
            $this->error = "Cannot connect to server.";
        }
    }
}
?>
</html>


So my question is if someone knows why it always updates with the first number of players instead of putting the new number of players?
Posted
Updated 11-Aug-15 10:22am
v2

1 solution

Your problem is many problems, and each problem can fail.

You have to have to device things in a way that insure that each part is OK.

You can fail to :
- refresh the display
- put the new value in page
- get the new number of players

For example, make page that display a counter every second. This way you will be sure that your page get refreshed on screen and that you can update a value.

-----

By using a counter for debugging, you have ensured that the page get refreshed because the counter changes each time.

Now by changing getplayers()
PHP
<?php
include "Status.php";
function getplayers() {
	return $counter;
}
?>
Do you have the number of players field updated with the counter ?
 
Share this answer
 
v3
Comments
JR37 11-Aug-15 16:56pm    
I can refresh the display and I can get the new number of players for sure.

I think there is something wrong with PHP which maybe doesn't let me update a variable being used after being loades in the page or something..
Patrice T 11-Aug-15 17:04pm    
Does it refresh with something new each time ? You will really know if something change each time.
JR37 11-Aug-15 17:05pm    
With an increasing number yes, but not with the new player count..
Patrice T 11-Aug-15 17:11pm    
So you have the knowledge that your page refresh on screen and that you can update part of the page with a counter.
It narrows the problem.

Now, if you copy the counter in the variable that hold the number of player (instead of geting from server), does it update on screen ?
JR37 11-Aug-15 17:16pm    
Sorry but I don't understand what you exactly mean. Could you try to explain it in another way?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900