82 lines
1.9 KiB
PHP
82 lines
1.9 KiB
PHP
<?php
|
|
// .env 파일을 한 번만 읽어 환경변수로 주입합니다.
|
|
function kngil_load_env_once(string $path): void
|
|
{
|
|
static $loaded = false;
|
|
if ($loaded) {
|
|
return;
|
|
}
|
|
|
|
$loaded = true;
|
|
|
|
if (!is_file($path) || !is_readable($path)) {
|
|
return;
|
|
}
|
|
|
|
$lines = file($path, FILE_IGNORE_NEW_LINES);
|
|
if ($lines === false) {
|
|
return;
|
|
}
|
|
|
|
foreach ($lines as $line) {
|
|
$line = trim($line);
|
|
if ($line === '' || str_starts_with($line, '#')) {
|
|
continue;
|
|
}
|
|
|
|
if (str_starts_with($line, 'export ')) {
|
|
$line = substr($line, 7);
|
|
}
|
|
|
|
$pos = strpos($line, '=');
|
|
if ($pos === false) {
|
|
continue;
|
|
}
|
|
|
|
$name = trim(substr($line, 0, $pos));
|
|
$value = trim(substr($line, $pos + 1));
|
|
|
|
if ($name === '') {
|
|
continue;
|
|
}
|
|
|
|
if (
|
|
(str_starts_with($value, '"') && str_ends_with($value, '"')) ||
|
|
(str_starts_with($value, "'") && str_ends_with($value, "'"))
|
|
) {
|
|
$value = substr($value, 1, -1);
|
|
}
|
|
|
|
$current = getenv($name);
|
|
if ($current !== false && $current !== '') {
|
|
continue;
|
|
}
|
|
|
|
putenv($name . '=' . $value);
|
|
$_ENV[$name] = $value;
|
|
$_SERVER[$name] = $value;
|
|
}
|
|
}
|
|
|
|
// 세션 쿠키 경로를 루트로 고정해 경로 변경 시 로그인 상태가 유지되도록 합니다.
|
|
function kngil_start_session(): void
|
|
{
|
|
if (session_status() !== PHP_SESSION_NONE) {
|
|
return;
|
|
}
|
|
|
|
$params = session_get_cookie_params();
|
|
$samesite = $params['samesite'] ?? 'Lax';
|
|
|
|
session_set_cookie_params([
|
|
'lifetime' => $params['lifetime'],
|
|
'path' => '/',
|
|
'domain' => $params['domain'],
|
|
'secure' => $params['secure'],
|
|
'httponly' => $params['httponly'],
|
|
'samesite' => $samesite,
|
|
]);
|
|
|
|
session_start();
|
|
}
|