first commit
This commit is contained in:
33
includes/auth.php
Normal file
33
includes/auth.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
session_start();
|
||||
require_once __DIR__.'/db.php';
|
||||
|
||||
// 登录状态检查
|
||||
function isLoggedIn() {
|
||||
return isset($_SESSION['user_id']);
|
||||
}
|
||||
|
||||
// 登录验证
|
||||
function login($username, $password) {
|
||||
global $pdo;
|
||||
|
||||
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
|
||||
$stmt->execute([$username]);
|
||||
$user = $stmt->fetch();
|
||||
|
||||
// SHA256验证
|
||||
if ($user && hash('sha256', $password) === $user['password']) {
|
||||
$_SESSION['user_id'] = $user['id'];
|
||||
$_SESSION['username'] = $user['username'];
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 强制登录检查
|
||||
function requireLogin() {
|
||||
if (!isLoggedIn()) {
|
||||
header("Location: login.php");
|
||||
exit;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user