119 lines
4.9 KiB
PHP
119 lines
4.9 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
session_start();
|
|
|
|
if (isset($_SESSION['user_id'])) {
|
|
header("Location: dashboard.php");
|
|
exit();
|
|
}
|
|
|
|
$errors = [];
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$username = trim($_POST['username'] ?? '');
|
|
$email = trim($_POST['email'] ?? '');
|
|
$password = $_POST['password'] ?? '';
|
|
|
|
// Validation
|
|
if (empty($username)) {
|
|
$errors[] = 'Username is required.';
|
|
}
|
|
if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$errors[] = 'A valid email is required.';
|
|
}
|
|
if (empty($password)) {
|
|
$errors[] = 'Password is required.';
|
|
} elseif (strlen($password) < 8) {
|
|
$errors[] = 'Password must be at least 8 characters long.';
|
|
}
|
|
|
|
if (empty($errors)) {
|
|
try {
|
|
$pdo = db();
|
|
// Check if username or email already exists
|
|
$stmt = $pdo->prepare("SELECT COUNT(*) FROM users WHERE email = :email OR username = :username");
|
|
$stmt->execute(['email' => $email, 'username' => $username]);
|
|
if ($stmt->fetchColumn() > 0) {
|
|
$errors[] = 'Username or email is already taken.';
|
|
} else {
|
|
// Hash password and insert new user
|
|
$password_hash = password_hash($password, PASSWORD_DEFAULT);
|
|
$insert_stmt = $pdo->prepare("INSERT INTO users (username, email, password_hash, role) VALUES (:username, :email, :password_hash, 'user')");
|
|
$insert_stmt->execute([
|
|
':username' => $username,
|
|
':email' => $email,
|
|
':password_hash' => $password_hash
|
|
]);
|
|
|
|
// Log the user in immediately
|
|
$_SESSION['user_id'] = $pdo->lastInsertId();
|
|
$_SESSION['username'] = $username;
|
|
$_SESSION['role'] = 'user';
|
|
|
|
header("Location: dashboard.php");
|
|
exit();
|
|
}
|
|
} catch (PDOException $e) {
|
|
error_log("Database error: " . $e->getMessage());
|
|
$errors[] = 'An internal error occurred. Please try again later.';
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Create Account - Car Sells in Afghanistan</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?v=<?php echo time(); ?>">
|
|
</head>
|
|
<body>
|
|
<div class="form-container">
|
|
<div class="card auth-card">
|
|
<div class="card-body">
|
|
<div class="text-center mb-5">
|
|
<h1 class="h2">Create Your Account</h1>
|
|
<p class="text-muted">Join us to find your dream car in Afghanistan.</p>
|
|
</div>
|
|
|
|
<?php if (!empty($errors)): ?>
|
|
<div class="alert alert-danger" role="alert">
|
|
<?php foreach ($errors as $error): ?>
|
|
<p class="mb-0"><?php echo htmlspecialchars($error); ?></p>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<form action="register.php" method="POST">
|
|
<div class="mb-3">
|
|
<label for="username" class="form-label">Username</label>
|
|
<input type="text" id="username" name="username" class="form-control" placeholder="e.g., ahmadwali" required value="<?php echo isset($username) ? htmlspecialchars($username) : ''; ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="email" class="form-label">Email address</label>
|
|
<input type="email" id="email" name="email" class="form-control" placeholder="you@example.com" required value="<?php echo isset($email) ? htmlspecialchars($email) : ''; ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="password" class="form-label">Password</label>
|
|
<input type="password" id="password" name="password" class="form-control" placeholder="Minimum 8 characters" required>
|
|
</div>
|
|
<div class="d-grid mt-4">
|
|
<button type="submit" class="btn btn-primary">Create Account</button>
|
|
</div>
|
|
</form>
|
|
|
|
<p class="text-center text-muted mt-4">
|
|
Already have an account? <a href="login.php">Login here</a>.
|
|
</p>
|
|
<p class="text-center text-muted mt-2">
|
|
<a href="index.php">Back to Home</a>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|