34943-vm/customer_signup_handler.php
Flatlogic Bot 0fcbb065ea 0001
2025-10-14 03:03:16 +00:00

50 lines
1.4 KiB
PHP

<?php
require_once 'db/config.php';
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: customer_signup.php');
exit;
}
// Basic validation
if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['password'])) {
die('Please fill all required fields.');
}
if (strlen($_POST['password']) < 8) {
die('Password must be at least 8 characters long.');
}
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
if (!$name || !$email) {
die('Invalid input.');
}
$pdo = db();
// Check if email already exists
$stmt = $pdo->prepare("SELECT id FROM users WHERE email = ?");
$stmt->execute([$email]);
if ($stmt->fetch()) {
die('An account with this email already exists. <a href="login.php">Log in here</a>.');
}
try {
// Create the user with the 'customer' role
$stmt_user = $pdo->prepare(
"INSERT INTO users (name, email, password, role) VALUES (?, ?, ?, 'customer')"
);
$stmt_user->execute([$name, $email, $password]);
// Redirect to the login page with a success message
header("Location: login.php?signup=success");
exit();
} catch (PDOException $e) {
// In a real app, you would log this error
die("Error creating account: " . $e->getMessage());
}