This commit is contained in:
2025-12-01 01:01:02 +01:00
parent d5b902c049
commit c2db8ddb63

View File

@@ -3,40 +3,38 @@
// 0) Umgebung / Domains / Error-Level
require_once __DIR__ . "/config.php";
// -----------------------------------------------------------
// Session starten (Frontend + API sollen dieselbe Session nutzen)
// ----------------------------------------------------------
// Session starten (gemeinsam für Frontend + API Subdomains)
// -----------------------------------------------------------
if (php_sapi_name() !== 'cli') {
if (session_status() === PHP_SESSION_NONE) {
// Einheitlicher Name für alle Teilbereiche
session_name('usbcheck_session');
// Cookie-Domain dynamisch bestimmen
$isHttps = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off');
// Cookie-Domain so wählen, dass ALLE Subdomains von usbcheck.it
// dieselbe Session sehen (staging., api.staging., www., …)
$cookieDomain = '';
if (!empty($_SERVER['HTTP_HOST'])) {
$host = $_SERVER['HTTP_HOST'];
// evtl. Port abschneiden
$host = preg_replace('/:\d+$/', '', $host);
// Für alle Subdomains von usbcheck.it dieselbe Session
if (preg_match('/\.?usbcheck\.it$/i', $host)) {
// alles was auf "usbcheck.it" endet, bekommt die gemeinsame Domain
if (substr($host, -strlen('usbcheck.it')) === 'usbcheck.it') {
// wirkt für usbcheck.it, staging.usbcheck.it, api.staging.usbcheck.it, ...
$cookieDomain = '.usbcheck.it';
}
// Falls du später andere Projekte auch per Subdomain teilen willst,
// kannst du hier weitere Regeln ergänzen, z.B.:
// elseif (preg_match('/\.?kusche\.berlin$/i', $host)) {
// $cookieDomain = '.kusche.berlin';
// }
}
session_set_cookie_params([
'lifetime' => 0,
'path' => '/',
'domain' => $cookieDomain, // WICHTIG: jetzt ggf. .usbcheck.it
'secure' => (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off'),
// WICHTIG: hier die Domain, damit API + Frontend teilen
'domain' => $cookieDomain ?: '',
'secure' => $isHttps,
'httponly' => true,
// eTLD+1 ist gleich (usbcheck.it), daher reicht Lax für Same-Site
'samesite' => 'Lax',
]);