35239-vm/register.php
Flatlogic Bot ccc924ba43 ver1
2025-10-26 14:24:09 +00:00

53 lines
1.8 KiB
PHP

<?php
require_once __DIR__ . '/db/config.php';
require_once __DIR__ . '/includes/session.php';
$errors = [];
$success_message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = trim($_POST['name'] ?? '');
$email = trim($_POST['email'] ?? '');
$password = $_POST['password'] ?? '';
$password_confirm = $_POST['password_confirm'] ?? '';
if (empty($name)) {
$errors[] = 'Name is required.';
}
if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = 'A valid email is required.';
}
if (empty($password)) {
$errors[] = 'Password is required.';
}
if ($password !== $password_confirm) {
$errors[] = 'Passwords do not match.';
}
if (empty($errors)) {
$pdo = db();
$stmt = $pdo->prepare("SELECT id FROM users WHERE email = ?");
$stmt->execute([$email]);
if ($stmt->fetch()) {
$errors[] = 'Email already in use.';
} else {
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$stmt = $pdo->prepare("INSERT INTO users (name, email, password) VALUES (?, ?, ?)");
if ($stmt->execute([$name, $email, $hashed_password])) {
$_SESSION['success_message'] = 'Registration successful! Please login.';
header('Location: /index.php#loginModal');
exit;
} else {
$errors[] = 'Something went wrong. Please try again.';
}
}
}
// To display errors, we would need to render a form here.
// For now, we redirect back to the form with errors in session.
$_SESSION['register_errors'] = $errors;
header('Location: /index.php#signupModal');
exit;
}
// This script does not render HTML. It only processes the form.
header('Location: /index.php');
exit;