Jump to content

include a php code within a forum description


Berserk

Recommended Posts

I'm not sure if i post this in the wrong section

i have this php code which retrives the server information and display them within a website

 

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once __DIR__ . '/vendor/autoload.php'; // Include Composer autoloader

use Knik\GRcon\GRcon;
use Knik\GRcon\Protocols\GoldSourceAdapter;

// Server details
$serverHost = '54.36.237.158';
$serverPort = '27015';
$rconPassword = 'test'; // Replace with your actual RCON password

// Retry parameters
$maxRetries = 3;      // Maximum number of retry attempts
$retryDelay = 2;     // Delay in seconds between retry attempts

for ($retry = 0; $retry < $maxRetries; $retry++) {
    try {
        // Create GoldSource RCON adapter
        $adapter = new GoldSourceAdapter([
            'host' => $serverHost,
            'port' => $serverPort,
            'password' => $rconPassword,
        ]);

        // Initialize RCON connection
        $rcon = new GRcon($adapter);

        // Attempt to execute RCON command to get server info
        $response = $rcon->execute('status');

        // Parse server response to extract information
        $serverInfo = [];
        preg_match('/hostname:\s+(.+)/', $response, $matches);
        if (isset($matches[1])) {
            $serverInfo['hostname'] = $matches[1];
        }
        preg_match('/map\s+:\s+(.+)/', $response, $matches);
        if (isset($matches[1])) {
            // Remove the "at: 0 x, 0 y, 0 z" part
            $serverInfo['map'] = preg_replace('/\sat:\s[0-9]+ x,\s[0-9]+ y,\s[0-9]+ z/', '', $matches[1]);
        }
        preg_match('/players\s+:\s+(\d+)/', $response, $matches);
        if (isset($matches[1])) {
            $serverInfo['playerCount'] = intval($matches[1]);
        }

        // Display server information
        echo "<h2>Server Name: " . $serverInfo['hostname'] . "</h2>";
        echo "<h3>Current Map: " . $serverInfo['map'] . "</h3>";
        echo "<h3>Player Count: " . $serverInfo['playerCount'] . "</h3>";

        break; // Connection and query were successful, exit retry loop
    } catch (Throwable $e) {
        echo "Error: " . $e->getMessage() . "<br>";

        if ($retry < $maxRetries - 1) {
            sleep($retryDelay); // Wait before retrying
        } else {
            echo "Unable to connect after $maxRetries retries.";
        }
    }
}
?>

the code is working fine , however i want to include this script in a phpbb forum description  something i can't figure out at all

Link to comment
Share on other sites

46 minutes ago, ginerjm said:

You "want to include this script in a phpbb forum description"?  Do you actually mean you want to publish your code as part of some text somewhere else?  Doesn't make much sense.

that code gives this result image.png.7d89e1b979b050fa79a011c35a99d7fe.png

 

In phpbb every forum allows you to put a description to it basically in want that in the description to be displayed the result of the code presented above

Link to comment
Share on other sites

There are two approaches that aren't unreasonable:

1. Make your thing render as an image, and embed the image into the description (assuming it supports HTML). Then whoever sees the description will see the image. Not great for SEO or visually-impaired users.
2. Set up a cronjob that runs a script every X minutes (whatever makes sense to you). This script calculates what it needs, then goes into the database and manipulates the forum's description text.

Link to comment
Share on other sites

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.