• Welcome to the Chevereto User Community!

    Here, users from all over the world come together to learn, share, and collaborate on everything related to Chevereto. It's a place to exchange ideas, ask questions, and help improve the software.

    Please keep in mind:

    • This community is user-driven. Always be polite and respectful to others.
    • Support development by purchasing a Chevereto license, which also gives you priority support.
    • Go further by joining the Community Subscription for even faster response times and to help sustain this space
  • Chevereto Support CLST

    Support response

    Support checklist

    • Got a Something went wrong message? Read this guide and provide the actual error. Do not skip this.
    • Confirm that the server meets the System Requirements
    • Check for any available Hotfix - your issue could be already reported/fixed
    • Read documentation - It will be required to Debug and understand Errors for a faster support response

How do I add custom functionality that is PHP backend related?

purplehead

Chevereto Member
Pro
I want to add something like a counter of how many are online in a little bubble bottom right.

I generated the JS required, which I know where to put, but how do I upload my PHP code for the session count? and not have it overwritten on an automatic update of the software? Can I access this information through the API? and would that be easier?

thanks

1758447688300.png

<?php
// online.php — returns {"count": N} based on recent PHP session files
header('Content-Type: application/json');

// how far back to consider "online"
$windowSeconds = 300; // 5 minutes

// session dir (defaults to php.ini's session.save_path)
$savePath = ini_get('session.save_path');
if (!$savePath) {
// common fallback; adjust if needed
$savePath = sys_get_temp_dir();
}

// count files modified within the window
$count = 0;
$now = time();
if (is_dir($savePath) && ($h = opendir($savePath))) {
while (($f = readdir($h)) !== false) {
if (strpos($f, 'sess_') === 0) {
$fp = $savePath . DIRECTORY_SEPARATOR . $f;
if (@filemtime($fp) >= ($now - $windowSeconds)) {
$count++;
}
}
}
closedir($h);
}

echo json_encode(['count' => $count], JSON_UNESCAPED_SLASHES);
 
You can use custom hooks.

so i just put that PHP code i have for the user session count, in content/legacy/themes/Peafowl/custom_hooks/ and it gets detected automatically?

I tried that but i dont get an end point, which i need for the js script?

Here is a chatgpt summery of what i tried, and what it said:

I tried adding a “users online” counter via Chevereto theme custom hooks, the built-in /api-v1, and even dropping PHP in theme hook paths and an internal page—each failed (404 or no JSON) because hooks don’t create backend endpoints and the image API doesn’t expose presence.


Options: (1) make a standalone PHP endpoint at the web root (e.g., /online.php) that returns {"count": N}, (2) add a custom Chevereto route (app/routes/overrides/route.online.php) to serve that JSON at /online, or (3) skip server code entirely and use a client-side presence service (e.g., Firebase Realtime Database) to track and display the live online count.
 
For this kind of need what you ideally want is to query the database to determine login activity, which will complete avoid the need for an extra endpoint and the really expensive opendir logic that chatgpt gave you, by no means you should be counting session files in a production system.

Problem is that for Chevereto sessions are persistent, it doesn't store the login "last time seen". I will add this attribute to the Login table for the next revision, that way to determine active users gets trivial as Login::countSessions() or something like that.
 
Back
Top