1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
<?php
declare(strict_types=1);
// Application bootstrap: loads .env, registers the autoloader, and configures error reporting.
// Harden session cookie: httponly prevents JS access (mitigates XSS stealing
// the session ID), samesite=Lax blocks cross-origin form submissions while
// still allowing normal link navigation, secure ensures the cookie is only
// sent over HTTPS (automatically detected from the request)
$isHttps = ($_SERVER['HTTPS'] ?? '') === 'on'
|| ($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '') === 'https';
session_set_cookie_params([
'httponly' => true,
'samesite' => 'Lax',
'secure' => $isHttps,
]);
session_start();
// Load .env
$envFile = dirname(__DIR__, 2) . '/.env';
if (file_exists($envFile)) {
$lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
if (str_starts_with(trim($line), '#')) {
continue;
}
$parts = explode('=', $line, 2);
if (count($parts) === 2) {
$key = trim($parts[0]);
$value = trim($parts[1]);
$_ENV[$key] = $value;
putenv("$key=$value");
}
}
}
// Autoloader
spl_autoload_register(function (string $class): void {
$prefix = 'App\\';
if (!str_starts_with($class, $prefix)) {
return;
}
$relative = substr($class, strlen($prefix));
$file = __DIR__ . '/' . str_replace('\\', '/', $relative) . '.php';
if (file_exists($file)) {
require $file;
}
});
// Error reporting
error_reporting(E_ALL);
ini_set('display_errors', '0');
ini_set('log_errors', '1');
|