31 lines
726 B
PHP
31 lines
726 B
PHP
<?php
|
|
// 简易会话认证
|
|
session_start();
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
function is_logged_in(): bool {
|
|
return !empty($_SESSION['logged_in']);
|
|
}
|
|
|
|
function require_login(): void {
|
|
if (!is_logged_in()) {
|
|
$target = 'login.php';
|
|
if (!headers_sent()) {
|
|
header('Location: ' . $target);
|
|
} else {
|
|
echo '<script>location.replace("' . $target . '")</script>';
|
|
echo '<noscript><meta http-equiv="refresh" content="0;url=' . $target . '"></noscript>';
|
|
}
|
|
exit;
|
|
}
|
|
}
|
|
|
|
function require_api_auth(): void {
|
|
if (!is_logged_in()) {
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
http_response_code(401);
|
|
echo json_encode(['error' => 'unauthorized']);
|
|
exit;
|
|
}
|
|
}
|