File manager - Edit - /home/webapp69.cm.in.th/u69319090024/Shop24/register.php
Back
<?php require_once __DIR__ . '/header.php'; // หากล็อกอินอยู่แล้ว ให้เปลี่ยนหน้าไปยัง index.php if (is_logged_in()) { header('Location: index.php'); exit; } $error = ''; $success = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $username = trim($_POST['username'] ?? ''); $fullname = trim($_POST['fullname'] ?? ''); $email = trim($_POST['email'] ?? ''); $password = $_POST['password'] ?? ''; $confirm_password = $_POST['confirm_password'] ?? ''; $role = (isset($_POST['role']) && $_POST['role'] === 'admin') ? 'admin' : 'user'; // ตรวจสอบความถูกต้องของข้อมูล (Validation) if (empty($username) || empty($fullname) || empty($email) || empty($password)) { $error = 'กรุณากรอกข้อมูลให้ครบทุกช่อง'; } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $error = 'รูปแบบอีเมลไม่ถูกต้อง'; } elseif (strlen($password) < 6) { $error = 'รหัสผ่านต้องมีความยาวอย่างน้อย 6 ตัวอักษร'; } elseif ($password !== $confirm_password) { $error = 'รหัสผ่านและการยืนยันรหัสผ่านไม่ตรงกัน'; } else { // ตรวจสอบว่ามี username หรือ email ซ้ำในระบบหรือไม่ (PDO Prepared Statement) $stmt = $pdo->prepare("SELECT id FROM users WHERE username = ? OR email = ?"); $stmt->execute([$username, $email]); if ($stmt->fetch()) { $error = 'ชื่อผู้ใช้ (Username) หรือ อีเมล นี้มีผู้ใช้งานแล้วในระบบ'; } else { // เข้ารหัสรหัสผ่านด้วย password_hash $hashed_password = password_hash($password, PASSWORD_DEFAULT); // บันทึกข้อมูลลงฐานข้อมูล $insert_stmt = $pdo->prepare("INSERT INTO users (username, password, fullname, email, role) VALUES (?, ?, ?, ?, ?)"); if ($insert_stmt->execute([$username, $hashed_password, $fullname, $email, $role])) { $new_user_id = $pdo->lastInsertId(); // เข้าสู่ระบบให้อัตโนมัติทันที session_regenerate_id(true); $_SESSION['user_id'] = $new_user_id; $_SESSION['username'] = $username; $_SESSION['fullname'] = $fullname; $_SESSION['email'] = $email; $_SESSION['role'] = $role; // จดจำการเข้าสู่ระบบผ่าน Cookie (30 วัน) $cookie_token = $new_user_id . ':' . md5($hashed_password . 'techshop_secure_salt'); setcookie('remember_token', $cookie_token, time() + (86400 * 30), "/", "", false, true); $_SESSION['welcome_msg'] = "สมัครสมาชิกและเข้าสู่ระบบเรียบร้อยแล้ว ยินดีต้อนรับคุณ {$fullname}!"; // หากเป็นแอดมินผู้ขายให้ไป admin_dashboard.php ไม่เช่นนั้นไป index.php หรือ cart.php if ($role === 'admin') { header('Location: admin_dashboard.php'); } elseif (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-6 col-lg-5"> <div class="card auth-card"> <div class="auth-header"> <i class="fa-solid fa-user-plus 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-triangle-exclamation me-2"></i> <?= htmlspecialchars($error) ?> <button type="button" class="btn-close" data-bs-dismiss="alert"></button> </div> <?php endif; ?> <?php if (!empty($success)): ?> <div class="alert alert-success alert-dismissible fade show rounded-3" role="alert"> <i class="fa-solid fa-circle-check me-2"></i> <?= htmlspecialchars($success) ?> <div class="mt-2"> <a href="login.php" class="btn btn-sm btn-success rounded-pill px-3">เข้าสู่ระบบทันที</a> </div> </div> <?php else: ?> <form method="POST" action="register.php"> <!-- Account Type Selection --> <div class="mb-3"> <label class="form-label fw-medium d-block">เลือกประเภทบัญชีที่ต้องการสมัคร</label> <div class="row g-2"> <div class="col-6"> <input type="radio" class="btn-check" name="role" id="role_user" value="user" checked> <label class="btn btn-outline-primary w-100 py-2 rounded-3 text-center" for="role_user"> <i class="fa-solid fa-cart-shopping d-block fs-5 mb-1"></i> <span class="fw-semibold small">🛒 ผู้ซื้อทั่วไป</span> </label> </div> <div class="col-6"> <input type="radio" class="btn-check" name="role" id="role_admin" value="admin"> <label class="btn btn-outline-warning text-dark w-100 py-2 rounded-3 text-center" for="role_admin"> <i class="fa-solid fa-store d-block fs-5 mb-1 text-warning"></i> <span class="fw-semibold small">👑 ร้านค้า / ผู้ขาย</span> </label> </div> </div> </div> <!-- Username --> <div class="mb-3"> <label class="form-label fw-medium">ชื่อผู้ใช้ (Username)</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" class="form-control" placeholder="เช่น user123 หรือ shop_admin" value="<?= htmlspecialchars($_POST['username'] ?? '') ?>" required> </div> </div> <!-- Fullname --> <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-id-card text-muted"></i></span> <input type="text" name="fullname" class="form-control" placeholder="เช่น สมชาย ใจดี" value="<?= htmlspecialchars($_POST['fullname'] ?? '') ?>" required> </div> </div> <!-- 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-envelope text-muted"></i></span> <input type="email" name="email" class="form-control" placeholder="example@domain.com" value="<?= htmlspecialchars($_POST['email'] ?? '') ?>" required> </div> </div> <!-- Password --> <div class="mb-3"> <label class="form-label fw-medium">รหัสผ่าน (Password)</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="อย่างน้อย 6 ตัวอักษร" required> </div> </div> <!-- Confirm Password --> <div class="mb-4"> <label class="form-label fw-medium">ยืนยันรหัสผ่าน</label> <div class="input-group"> <span class="input-group-text"><i class="fa-solid fa-shield-halved text-muted"></i></span> <input type="password" name="confirm_password" class="form-control" placeholder="กรอกรหัสผ่านซ้ำอีกครั้ง" required> </div> </div> <button type="submit" class="btn btn-primary-custom w-100 py-2 rounded-pill fw-semibold"> <i class="fa-solid fa-check-circle me-1"></i> ยืนยันการสมัครสมาชิก </button> </form> <?php endif; ?> <div class="text-center mt-4 pt-3 border-top"> <span class="text-muted small">มีบัญชีผู้ใช้อยู่แล้ว?</span> <a href="login.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.4 |
proxy
|
phpinfo
|
Settings