<?php
/**
 * Session Diagnostic - Upload this to your server and visit /lms/session_test.php
 */
ini_set('display_errors', 1);
error_reporting(E_ALL);

echo "<pre>";
echo "=== SESSION DIAGNOSTIC ===\n\n";

echo "1. Output buffering level: " . ob_get_level() . "\n";
echo "2. Headers sent: " . (headers_sent() ? 'YES' : 'NO') . "\n";
echo "3. Session auto_start: " . ini_get('session.auto_start') . "\n";
echo "4. Session status BEFORE start: ";
$status = session_status();
echo ($status === PHP_SESSION_NONE ? 'NONE' : ($status === PHP_SESSION_ACTIVE ? 'ACTIVE' : 'DISABLED')) . "\n";

echo "5. Session save_path: " . ini_get('session.save_path') . "\n";
echo "6. save_path writable: " . (is_writable(ini_get('session.save_path')) ? 'YES' : 'NO') . "\n";

// Try to start session
try {
    session_start();
    echo "7. Session started: YES\n";
    echo "8. Session ID: " . session_id() . "\n";
    
    $_SESSION['test'] = 'works';
    echo "9. Can write to \$_SESSION: YES\n";
    echo "10. \$_SESSION['test'] = " . $_SESSION['test'] . "\n";
    
    echo "\n=== COOKIE INFO ===\n";
    print_r(session_get_cookie_params());
    
    echo "\n=== SENT HEADERS ===\n";
    $headers = headers_list();
    foreach ($headers as $h) {
        if (stripos($h, 'Set-Cookie') !== false || stripos($h, 'session') !== false) {
            echo $h . "\n";
        }
    }
    
} catch (Throwable $e) {
    echo "7. Session started: NO - " . $e->getMessage() . "\n";
}

echo "\n=== OUTPUT BEFORE THIS POINT ===\n";
echo "Any unexpected output above this line would cause 'headers already sent'\n";

echo "</pre>";
