<?php
session_start();

header('Content-Type: text/html; charset=UTF-8');

echo '<h1>Session Cookie Debug</h1>';

echo '<h3>1. Session Information:</h3>';
echo '<p>Session ID: <code>' . session_id() . '</code></p>';
echo '<p>Session Save Path: <code>' . ini_get('session.save_path') . '</code></p>';
echo '<p>Session Status: ' . (session_status() === PHP_SESSION_ACTIVE ? 'ACTIVE' : 'INACTIVE') . '</p>';

echo '<h3>2. Cookies Sent by Server:</h3>';
echo '<p>headers_list():</p>';
echo '<pre>';
foreach (headers_list() as $header) {
    if (strpos(strtolower($header), 'set-cookie') !== false) {
        echo htmlspecialchars($header) . "\n";
    }
}
echo '</pre>';

echo '<h3>3. $_COOKIE Array:</h3>';
echo '<pre>';
var_dump($_COOKIE);
echo '</pre>';

echo '<h3>4. Test Setting Session Value:</h3>';
$_SESSION['test'] = 'VALUE_' . time();
session_write_close();
echo '<p>Set $_SESSION["test"] = "VALUE_' . time() . '"</p>';

echo '<h3>5. Session File Location:</h3>';
$sessionDir = session_save_path();
echo '<p>Session save dir: ' . htmlspecialchars($sessionDir) . '</p>';
if (is_dir($sessionDir)) {
    echo '<p style="color: green;">✓ Directory exists and is writable</p>';
} else {
    echo '<p style="color: red;">✗ Directory does NOT exist or is NOT writable!</p>';
}

// Try to list session files
echo '<h3>6. Session Files in Directory:</h3>';
$files = glob($sessionDir . '/sess_*');
if (!empty($files)) {
    echo '<p>Found ' . count($files) . ' session files</p>';
    // Show latest 5
    usort($files, function($a, $b) { return filemtime($b) - filemtime($a); });
    foreach (array_slice($files, 0, 5) as $file) {
        $basename = basename($file);
        $mtime = date('Y-m-d H:i:s', filemtime($file));
        echo '<p>   ' . htmlspecialchars($basename) . ' (' . $mtime . ')</p>';
    }
} else {
    echo '<p style="color: red;">✗ No session files found in save directory!</p>';
}
?>
