码桶

发现社区成员的开源项目

main
domains/admin/index.php
index.php2.4 KB
<?php
session_start();
require_once 'config.php';

// 如果已经登录,直接跳回首页
if (isset($_SESSION['is_admin']) && $_SESSION['is_admin'] === true) {
    header('Location: /');
    exit;
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $user = $_POST['username'] ?? '';
    $pass = $_POST['password'] ?? '';

    if ($user === $ADMIN_USER && $pass === $ADMIN_PASS) {
        // 登录成功,设置安全Session
        $_SESSION['is_admin'] = true;
        
        // 动态获取当前后台文件夹名,用于前端拼接API路径
        $current_dir = basename(__DIR__);
        
        // 设置Cookie给前端识别
        setcookie('admin_auth', '1', time() + 86400, '/'); // 1天有效期
        setcookie('admin_path', $current_dir, time() + 86400, '/');
        
        header('Location: /');
        exit;
    } else {
        $error = '账号或密码错误';
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理员登录</title>
    <style>
        body { background: #050507; color: #f5f5f7; font-family: sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; }
        .login-box { background: rgba(20, 20, 24, 0.8); padding: 40px; border-radius: 20px; border: 1px solid rgba(229, 192, 123, 0.2); width: 320px; }
        h2 { text-align: center; color: #E5C07B; margin-bottom: 30px; }
        input { width: 100%; padding: 12px; margin-bottom: 15px; background: rgba(30, 30, 34, 0.8); border: 1px solid rgba(255,255,255,0.1); border-radius: 8px; color: #fff; box-sizing: border-box; }
        button { width: 100%; padding: 12px; background: #E5C07B; color: #050507; border: none; border-radius: 8px; font-weight: bold; cursor: pointer; }
        .error { color: #ff4d4f; text-align: center; margin-bottom: 15px; font-size: 14px; }
    </style>
</head>
<body>
    <div class="login-box">
        <h2>瀚海星辰 管理后台</h2>
        <?php if($error): ?><div class="error"><?php echo $error; ?></div><?php endif; ?>
        <form method="POST">
            <input type="text" name="username" placeholder="账号" required autofocus>
            <input type="password" name="password" placeholder="密码" required>
            <button type="submit">登录</button>
        </form>
    </div>
</body>
</html>