IsLoggedIn() keeps throwing error "Call to a member function get() on null"

I use the following code as a custom file without the common files. I know - that’s not recommended - but I need some custom files that return data in a specific format. I include this file in all of them to deny access to users who aren’t logged in.

The function IsLoggedIn() keeps returning this error:
Fatal error: Uncaught Error: Call to a member function get() on null in C:\xampp\htdocs\myapp\app\src\phpfn.php:4982

It seems that, even though I start a session, no session is being created.

My code:

/* Use defaults from index.php to get access to database and PHPMaker functions */
namespace PHPMaker2025\myapp;
use PHPMaker2025\myapp\{UserProfile, Language, AdvancedSecurity, Timer, HttpErrorHandler};

$RELATIVE_PATH = "../";

require_once $RELATIVE_PATH . "vendor/autoload.php";
require_once $RELATIVE_PATH . "src/constants.php";
require_once $RELATIVE_PATH . "src/config.php";
require_once $RELATIVE_PATH . "src/phpfn.php";
require_once $RELATIVE_PATH . "src/userfn.php";


// Start Session if not started yet
if (session_status() !== PHP_SESSION_ACTIVE) {
	session_start(); // Init session data
}

/* Deny access if user is not logged in */
if(!IsLoggedIn()) die('Not logged in. Permission denied.');

Does anybody know why or have a working example?

This might help: How to Load Data from Database into Custom Files (v2025)

Avoid using separate files and trying to use them directly bypassing the web application, this will create many unexpected problems and cannot be carried to future versions which usually have changes. You may want to read Using Route_Action server event (v2021). If you prefer custom file, you may add your own controller.

Could anybody provide me with a sample project? I can’t get the routes to work in custom files.

See this post as the simple Custom File that disables Include common files option: