36018-vm/signup.php
2025-11-22 13:52:11 +00:00

109 lines
3.4 KiB
PHP

<?php
session_start();
require_once __DIR__ . '/db/config.php';
$errors = [];
$full_name = '';
$email = '';
$phone = '';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$full_name = trim($_POST['full_name']);
$email = trim($_POST['email']);
$phone = trim($_POST['phone']);
$password = $_POST['password'];
$confirm_password = $_POST['confirm_password'];
if (empty($full_name)) {
$errors[] = 'Full name is required.';
}
if (empty($email)) {
$errors[] = 'Email is required.';
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Invalid email format.';
}
if (empty($phone)) {
$errors[] = 'Phone number is required.';
}
if (empty($password)) {
$errors[] = 'Password is required.';
}
if ($password !== $confirm_password) {
$errors[] = 'Passwords do not match.';
}
if (empty($errors)) {
try {
$pdo = db();
$stmt = $pdo->prepare("SELECT * FROM users WHERE email = ?");
$stmt->execute([$email]);
$existing_user = $stmt->fetch();
if ($existing_user) {
$errors[] = 'A user with this email already exists.';
} else {
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$stmt = $pdo->prepare("INSERT INTO users (full_name, email, phone, password) VALUES (?, ?, ?, ?)");
$stmt->execute([$full_name, $email, $phone, $hashed_password]);
header("Location: login.php?registration=success");
exit;
}
} catch (PDOException $e) {
// In a real app, you would log this error, not show it to the user.
$errors[] = "Database error. Please try again later.";
}
}
}
$pageTitle = 'Sign Up';
require_once __DIR__ . '/shared/header.php';
?>
<div class="form-container">
<h1>Create an Account</h1>
<?php if (!empty($errors)): ?>
<div class="errors">
<?php foreach ($errors as $error): ?>
<p><?php echo htmlspecialchars($error); ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<form action="signup.php" method="post">
<div class="form-group">
<label for="full_name">Full Name</label>
<input type="text" id="full_name" name="full_name" required value="<?php echo htmlspecialchars($full_name); ?>">
</div>
<div class="form-group">
<label for="email">Email Address</label>
<input type="email" id="email" name="email" required value="<?php echo htmlspecialchars($email); ?>">
</div>
<div class="form-group">
<label for="phone">Phone Number</label>
<input type="tel" id="phone" name="phone" required value="<?php echo htmlspecialchars($phone); ?>">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="password" name="password" required>
</div>
<div class="form-group">
<label for="confirm_password">Confirm Password</label>
<input type="password" id="confirm_password" name="confirm_password" required>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary" style="width: 100%;">Sign Up</button>
</div>
</form>
</div>
<?php
require_once __DIR__ . '/shared/footer.php';
?>