Sindbad~EG File Manager

Current Path : /home/beeson/public_html/WebMathXpert/
Upload File :
Current File : //home/beeson/public_html/WebMathXpert/userlog.php

<?php
// Ensure this script does not output anything
if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}
// Set timezone to Pacific Time (change as needed)
date_default_timezone_set('America/Los_Angeles'); 

// Path to the log file
define("LOG_FILE", "/usr/home/beeson/MathXpert/logs/userlog.csv");

// Function to get user's IP address (handles IPv4 and IPv6)
function getUserIP() {
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        return $_SERVER['HTTP_CLIENT_IP']; // Shared internet
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        return trim(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])[0]); // Proxy
    } else {
        return $_SERVER['REMOTE_ADDR']; // Direct connection
    }
}

// Function to log a new session
function logNewSession() {
    $sessionID = session_id();
    $userIP = getUserIP();
    $timestamp = date("Y-m-d,H:i:s");

    // Prepare log entry
    $logEntry = "$timestamp,$sessionID,$userIP\n";

    // Attempt to write to the log file
    file_put_contents(LOG_FILE, $logEntry, FILE_APPEND | LOCK_EX); 
    // if we are on localhost the file doesn't exist, but this fails 
    // gracefully.  We could check if the file exists, but that would 
    // add milliseconds to the the delivery of the first page from the server, ...
    // so when we're on localhost, we just try to write and fail. 
    
    // Mark session as started
    $_SESSION['user_started'] = true;
}
?>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists