36874-vm/register.php
Flatlogic Bot 1d182fef38 1.1
2025-12-11 19:19:52 +00:00

103 lines
4.1 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
$errors = [];
$name = '';
$email = '';
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';
} else {
$stmt = db()->prepare("SELECT id FROM users WHERE email = ?");
$stmt->execute([$email]);
if ($stmt->fetch()) {
$errors[] = 'Email already in use';
}
}
if (empty($password)) {
$errors[] = 'Password is required';
}
if ($password !== $password_confirm) {
$errors[] = 'Passwords do not match';
}
if (empty($errors)) {
$role = isset($_POST['is_vendor']) && $_POST['is_vendor'] == '1' ? 'vendor' : 'user';
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$stmt = db()->prepare("INSERT INTO users (name, email, password, role) VALUES (?, ?, ?, ?)");
if ($stmt->execute([$name, $email, $hashed_password, $role])) {
// Redirect to login page
header("Location: login.php?registered=true");
exit;
} else {
$errors[] = "Failed to create account. Please try again.";
}
}
}
?>
<?php include 'header.php'; ?>
<div class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card">
<div class="card-header">
<h4>Create an Account</h4>
</div>
<div class="card-body">
<?php if (!empty($errors)): ?>
<div class="alert alert-danger">
<?php foreach ($errors as $error): ?>
<p><?php echo $error; ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<form action="register.php" method="POST">
<div class="mb-3">
<label for="name" class="form-label">Full Name</label>
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($name); ?>" required>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email Address</label>
<input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($email); ?>" required>
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<div class="mb-3">
<label for="password_confirm" class="form-label">Confirm Password</label>
<input type="password" class="form-control" id="password_confirm" name="password_confirm" required>
</div>
<div class="form-check mb-3">
<input class="form-check-input" type="checkbox" value="1" id="is_vendor" name="is_vendor">
<label class="form-check-label" for="is_vendor">
I want to be a vendor
</label>
</div>
<button type="submit" class="btn btn-primary w-100">Register</button>
</form>
</div>
<div class="card-footer text-center">
Already have an account? <a href="login.php">Login here</a>
</div>
</div>
</div>
</div>
</div>
<?php include 'footer.php'; ?>