File manager - Edit - /home/webapp69.cm.in.th/u69319090024/Shop24/login.php
Back
<?php require_once __DIR__ . '/header.php'; // หากล็อกอินอยู่แล้ว ให้เปลี่ยนหน้าไปยัง index.php if (is_logged_in()) { header('Location: index.php'); exit; } $error = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $username_or_email = trim($_POST['username_or_email'] ?? ''); $password = $_POST['password'] ?? ''; if (empty($username_or_email) || empty($password)) { $error = 'กรุณากรอกชื่อผู้ใช้/อีเมล และรหัสผ่าน'; } else { // ค้นหาผู้ใช้ในฐานข้อมูลด้วย PDO Prepared Statement $stmt = $pdo->prepare("SELECT * FROM users WHERE username = ? OR email = ? LIMIT 1"); $stmt->execute([$username_or_email, $username_or_email]); $user = $stmt->fetch(); // ตรวจสอบรหัสผ่านด้วย password_verify if ($user && password_verify($password, $user['password'])) { // เพื่อความปลอดภัย ป้องกัน Session Fixation session_regenerate_id(true); // บันทึกข้อมูลลง Session $_SESSION['user_id'] = $user['id']; $_SESSION['username'] = $user['username']; $_SESSION['fullname'] = $user['fullname']; $_SESSION['email'] = $user['email']; $_SESSION['role'] = $user['role'] ?? 'user'; // จัดการ Cookie ระบบจดจำการเข้าสู่ระบบ (Remember Me) if (isset($_POST['remember_me'])) { $cookie_token = $user['id'] . ':' . md5($user['password'] . 'techshop_secure_salt'); setcookie('remember_token', $cookie_token, time() + (86400 * 30), "/", "", false, true); } else { setcookie('remember_token', '', time() - 3600, "/"); } // หากเป็น Admin ให้เปลี่ยนหน้าไปยัง Admin Dashboard if ($user['role'] === 'admin') { header('Location: admin_dashboard.php'); exit; } // เปลี่ยนหน้าไปยัง cart.php หากมีสินค้าในตะกร้า หรือไปยัง index.php if (get_cart_count() > 0) { header('Location: cart.php'); } else { header('Location: index.php'); } exit; } else { $error = 'ชื่อผู้ใช้/อีเมล หรือรหัสผ่านไม่ถูกต้อง'; } } } ?> <div class="row justify-content-center py-4"> <div class="col-md-5 col-lg-4"> <div class="card auth-card"> <div class="auth-header"> <i class="fa-solid fa-right-to-bracket display-4 mb-2"></i> <h4 class="fw-bold mb-0">เข้าสู่ระบบ</h4> <p class="small text-white-50 mb-0">ยินดีต้อนรับสู่ระบบร้านค้าออนไลน์</p> </div> <div class="card-body p-4"> <?php if (!empty($error)): ?> <div class="alert alert-danger alert-dismissible fade show rounded-3" role="alert"> <i class="fa-solid fa-circle-exclamation me-2"></i> <?= htmlspecialchars($error) ?> <button type="button" class="btn-close" data-bs-dismiss="alert"></button> </div> <?php endif; ?> <form method="POST" action="login.php"> <!-- Username or Email --> <div class="mb-3"> <label class="form-label fw-medium">ชื่อผู้ใช้ หรือ อีเมล</label> <div class="input-group"> <span class="input-group-text"><i class="fa-solid fa-user text-muted"></i></span> <input type="text" name="username_or_email" class="form-control" placeholder="username หรือ email@domain.com" value="<?= htmlspecialchars($_POST['username_or_email'] ?? '') ?>" required> </div> </div> <!-- Password --> <div class="mb-3"> <label class="form-label fw-medium">รหัสผ่าน</label> <div class="input-group"> <span class="input-group-text"><i class="fa-solid fa-lock text-muted"></i></span> <input type="password" name="password" class="form-control" placeholder="กรอกรหัสผ่าน" value="" required> </div> </div> <!-- Remember Me Checkbox --> <div class="mb-4 form-check"> <input type="checkbox" name="remember_me" class="form-check-input" id="rememberMe" checked> <label class="form-check-label small fw-medium text-secondary" for="rememberMe"> <i class="fa-solid fa-shield-halved me-1 text-primary"></i>จดจำการเข้าสู่ระบบ (Remember Me 30 วัน) </label> </div> <button type="submit" class="btn btn-primary-custom w-100 py-2 rounded-pill fw-semibold mb-3"> <i class="fa-solid fa-right-to-bracket me-1"></i> เข้าสู่ระบบ </button> </form> <!-- Demo Account Box พร้อมปุ่มกดเติมข้อมูลทางลัด --> <div class="bg-light p-3 rounded-3 border"> <small class="text-muted d-block fw-semibold mb-2"><i class="fa-solid fa-key text-primary me-1"></i>บัญชีทดสอบระบบ (คลิกเพื่อเติมข้อมูลทันที):</small> <div class="d-grid gap-2"> <button type="button" class="btn btn-outline-warning btn-sm text-dark text-start rounded-pill px-3" onclick="fillAccount('admin', 'password123');"> <i class="fa-solid fa-crown text-warning me-1"></i> <strong>เข้าสู่ระบบผู้ขาย/แอดมิน:</strong> <code>admin</code> / <code>password123</code> </button> <button type="button" class="btn btn-outline-secondary btn-sm text-start rounded-pill px-3" onclick="fillAccount('demo', 'password123');"> <i class="fa-solid fa-user text-primary me-1"></i> <strong>เข้าสู่ระบบผู้ซื้อ:</strong> <code>demo</code> / <code>password123</code> </button> </div> </div> <script> function fillAccount(username, password) { document.querySelector('input[name="username_or_email"]').value = username; document.querySelector('input[name="password"]').value = password; } </script> <div class="text-center mt-4 pt-3 border-top"> <span class="text-muted small">ยังไม่มีบัญชีผู้ใช้?</span> <a href="register.php" class="fw-semibold text-primary text-decoration-none small ms-1">สมัครสมาชิกที่นี่</a> </div> </div> </div> </div> </div> <?php require_once __DIR__ . '/footer.php'; ?>
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.31 |
proxy
|
phpinfo
|
Settings