This commit is contained in:
2025-12-01 00:42:42 +01:00
parent 2f2c73c774
commit 48aab5c033
2 changed files with 112 additions and 109 deletions

View File

@@ -36,10 +36,28 @@ function browser_quick_test_handle_request(): array
} }
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
// 2. User / Session ermitteln // 2. User / Session ermitteln (robuster)
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
$userId = $_SESSION['user_id'] ?? null; // abhängig von deiner Login-Implementierung $userId = null;
$isLoggedIn = $userId ? 1 : 0; $isLoggedIn = 0;
// Variante A: klassisch
if (!empty($_SESSION['user_id'])) {
$userId = (int)$_SESSION['user_id'];
}
// Variante B: User-Array in der Session (z.B. $_SESSION['user']['id'])
elseif (!empty($_SESSION['user']) && is_array($_SESSION['user']) && !empty($_SESSION['user']['id'])) {
$userId = (int)$_SESSION['user']['id'];
}
// Variante C: auth-Block (z.B. $_SESSION['auth']['user_id'])
elseif (!empty($_SESSION['auth']) && is_array($_SESSION['auth']) && !empty($_SESSION['auth']['user_id'])) {
$userId = (int)$_SESSION['auth']['user_id'];
}
if ($userId) {
$isLoggedIn = 1;
}
$sessionId = session_id() ?: null; $sessionId = session_id() ?: null;
$ipAddress = $_SERVER['REMOTE_ADDR'] ?? null; $ipAddress = $_SERVER['REMOTE_ADDR'] ?? null;
@@ -160,7 +178,6 @@ $stmt->execute([
'session_id' => $sessionId, 'session_id' => $sessionId,
]); ]);
$id = (int)$pdo->lastInsertId(); $id = (int)$pdo->lastInsertId();
return [ return [

View File

@@ -4,61 +4,47 @@
require_once __DIR__ . "/config.php"; require_once __DIR__ . "/config.php";
// ----------------------------------------------------------- // -----------------------------------------------------------
// Session starten (gemeinsam für Frontend + API) // Session starten (Frontend + API sollen dieselbe Session nutzen)
// ----------------------------------------------------------- // -----------------------------------------------------------
if (php_sapi_name() !== 'cli') { if (php_sapi_name() !== 'cli') {
if (session_status() === PHP_SESSION_NONE) { if (session_status() === PHP_SESSION_NONE) {
// Host ermitteln session_name('usbcheck_session');
$host = $_SERVER['HTTP_HOST'] ?? '';
/** // Cookie-Domain dynamisch bestimmen
* Ziel:
* STAGING:
* - staging.usbcheck.it
* - api.staging.usbcheck.it
* -> Cookie-Domain: .staging.usbcheck.it
*
* PROD:
* - usbcheck.it
* - www.usbcheck.it
* - api.usbcheck.it
* -> Cookie-Domain: .usbcheck.it
*
* LOKAL/SONSTIG:
* - z.B. localhost, 127.0.0.1
* -> Keine Domain setzen (Browser nimmt Host)
*/
$cookieDomain = ''; $cookieDomain = '';
if (!empty($_SERVER['HTTP_HOST'])) {
$host = $_SERVER['HTTP_HOST'];
// evtl. Port abschneiden
$host = preg_replace('/:\d+$/', '', $host);
if (preg_match('~\.staging\.usbcheck\.it$~', $host)) { // Für alle Subdomains von usbcheck.it dieselbe Session
// alles unter *.staging.usbcheck.it if (preg_match('/\.?usbcheck\.it$/i', $host)) {
$cookieDomain = '.staging.usbcheck.it'; // wirkt für usbcheck.it, staging.usbcheck.it, api.staging.usbcheck.it, ...
} elseif (preg_match('~(^|\.)(usbcheck\.it)$~', $host)) {
// usbcheck.it, www.usbcheck.it, api.usbcheck.it, ...
$cookieDomain = '.usbcheck.it'; $cookieDomain = '.usbcheck.it';
} else {
// z.B. localhost → leer lassen
$cookieDomain = '';
} }
// Einheitlicher Session-Name für alle usbcheck-Hosts // Falls du später andere Projekte auch per Subdomain teilen willst,
session_name('usbcheck_session'); // kannst du hier weitere Regeln ergänzen, z.B.:
// elseif (preg_match('/\.?kusche\.berlin$/i', $host)) {
// $cookieDomain = '.kusche.berlin';
// }
}
session_set_cookie_params([ session_set_cookie_params([
'lifetime' => 0, 'lifetime' => 0,
'path' => '/', 'path' => '/',
'domain' => $cookieDomain, // wichtig für gemeinsame Session über Subdomains 'domain' => $cookieDomain, // WICHTIG: jetzt ggf. .usbcheck.it
'secure' => (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off'), 'secure' => (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off'),
'httponly' => true, 'httponly' => true,
'samesite' => 'Lax', // reicht für gleiche Site (staging/api.*.usbcheck.it) 'samesite' => 'Lax',
]); ]);
session_start(); session_start();
} }
} }
require_once __DIR__ . '/i18n.php'; // zentrale Sprachlogik require_once __DIR__ . '/i18n.php'; // <— zentrale Sprachlogik
// ab hier kannst du überall $GLOBALS['lang'] und $GLOBALS['availableLangs'] nutzen // ab hier kannst du überall $GLOBALS['lang'] und $GLOBALS['availableLangs'] nutzen
// und für JS: // und für JS: