35067-vm/register.php
Flatlogic Bot b200618254 eco-grow
2025-10-20 06:45:18 +00:00

93 lines
3.7 KiB
PHP

<?php
require_once 'db/config.php';
$message = '';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$password = $_POST['password'];
$role = 'customer'; // Default role for new users
if (empty($name) || empty($email) || empty($password)) {
$message = "Please fill out all fields.";
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$message = "Invalid email format.";
} else {
try {
$pdo = db();
// Check if email already exists
$stmt = $pdo->prepare("SELECT * FROM users WHERE email = ?");
$stmt->execute([$email]);
if ($stmt->fetch()) {
$message = "Email already registered.";
} else {
// Hash the password for security
$password_hash = password_hash($password, PASSWORD_BCRYPT);
// Insert the new user into the database
$stmt = $pdo->prepare("INSERT INTO users (name, email, password, role) VALUES (?, ?, ?, ?)");
if ($stmt->execute([$name, $email, $password_hash, $role])) {
header("Location: login.php?registered=true");
exit;
} else {
$message = "An error occurred. Please try again.";
}
}
} catch (PDOException $e) {
// In a real app, you'd log this error, not show it to the user.
$message = "Database error: " . $e->getMessage();
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Register</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css">
</head>
<body>
<?php include 'includes/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">
<h2>Register</h2>
</div>
<div class="card-body">
<?php if ($message): ?>
<div class="alert alert-danger"><?php echo htmlspecialchars($message); ?></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" 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" 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>
<button type="submit" class="btn btn-primary">Register</button>
</form>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>