-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
61 lines (58 loc) · 2.62 KB
/
index.php
File metadata and controls
61 lines (58 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
require_once 'config.php';
if (isset($_SESSION['user_id'])) {
header("Location: dashboard.php");
exit;
}
$error = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (!validateCsrf($_POST['csrf_token'])) {
$error = 'Invalid CSRF token.';
} else {
$username = $_POST['username'] ?? '';
$password = $_POST['password'] ?? '';
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
$stmt->execute([$username]);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password'])) {
session_regenerate_id(true);
$_SESSION['user_id'] = $user['id'];
$_SESSION['fullname'] = $user['fullname'];
$_SESSION['role'] = $user['role'];
header("Location: dashboard.php");
exit;
} else {
$error = 'ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง (Default: admin / admin1234)';
}
}
}
?>
<!DOCTYPE html>
<html lang="th">
<head>
<meta charset="UTF-8">
<title>เข้าสู่ระบบ - ระบบบันทึกผู้ติดต่อ</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 flex items-center justify-center h-screen">
<div class="bg-white p-8 rounded shadow-md w-full max-w-md">
<h2 class="text-2xl font-bold mb-6 text-center text-blue-600">ระบบบันทึกการเข้า-ออก</h2>
<?php if($error): ?><div class="bg-red-100 text-red-700 p-2 mb-4 rounded text-sm"><?php echo htmlspecialchars($error); ?></div><?php endif; ?>
<form method="POST">
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']; ?>">
<div class="mb-4">
<label class="block text-gray-700">ชื่อผู้ใช้งาน</label>
<input type="text" name="username" required class="w-full border rounded p-2 mt-1">
</div>
<div class="mb-6">
<label class="block text-gray-700">รหัสผ่าน</label>
<input type="password" name="password" required class="w-full border rounded p-2 mt-1">
</div>
<button type="submit" class="w-full bg-blue-600 text-white p-2 rounded hover:bg-blue-700 transition">เข้าสู่ระบบ</button>
</form>
<p class="mt-4 text-center text-sm">
<a href="register.php" class="text-blue-500 hover:underline">สมัครสมาชิกใหม่</a>
</p>
</div>
</body>
</html>