初始化版本
This commit is contained in:
30
auth.php
Normal file
30
auth.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user