Check if players are online using PHP

User avatar
MangleFox70
Member
 
Posts: 61
Joined: Mon Feb 01, 2016 14:50
GitHub: Cat5TV
IRC: MangleFox70
In-game: MangleFox70

Check if players are online using PHP

by MangleFox70 » Wed Feb 10, 2016 03:31

Here's another freebie from Minetest.TV...

This one looks at your active log file and checks the last instance of the player list. It then outputs the player count.

Because it reports no users as 0, you can run your existing Linux scripts against this to know if it's [for example] safe to restart Minetest for a backup. Eg., PLAYERS=`php player_count.php` in a bash script will set $PLAYERS accordingly. Or, do what we do: comment out the two "echo" lines and include() the script in our main PHP script, and then refer to $playercount any time you want to make a decision based on number of online players. if ($playercount > 0) { // do nothing } else { // do something } kind of idea.

All you have to do is change the log location, and be sure to install PHP (doesn't need to be a web server, just need the interpreter... php5 for example)

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
<?php
  $logfile = '/home/robbie/.minetest/logs/server.log';
  $logdata = file($logfile);
  if (is_array($logdata)) {
    $logdata = array_reverse($logdata);
    foreach ($logdata as $logline) {
      if (strstr($logline,'List of players:')) {
        $tmp = explode('List of players:',$logline);
        $players = trim($tmp[1]);
        if (strstr($players,' ')) {
          $tmp2 = explode(' ',$players);
          $playercount = count($tmp2);
        } elseif (strlen($players) > 0) {
          $playercount = 1;
        } else {
          $playercount = 0;
        }
        break;
      }
    }
    echo $playercount;
  } else {
    echo 'Unable to load log.';
  }
?>


Enjoy! If you use this in your project, please link to it below -- we'd love to know.

We use this script to know if it's safe to restart Minetest and sync our player skins which have been uploaded to our web site, which is a fully automated process.
#ThePixelShadow on YouTube
A Weekly Minetest Webcast hosted by MangleFox70
Have a mod you'd like featured? Want to participate in our show? Join servers.minetest.tv Port 30000 for creative or 30001 for survival.
Upload your own skin via our web site: Minetest.TV
 

Return to Minetest-Related

Who is online

Users browsing this forum: No registered users and 21 guests

cron