109 lines
4.6 KiB
PHP
109 lines
4.6 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
session_start();
|
|
|
|
if (isset($_SESSION['user_id'])) {
|
|
header("Location: index.php");
|
|
exit();
|
|
}
|
|
|
|
$errors = [];
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$username = trim($_POST['username']);
|
|
$email = trim($_POST['email']);
|
|
$password = $_POST['password'];
|
|
|
|
if (empty($username)) {
|
|
$errors[] = 'Username is required';
|
|
}
|
|
if (empty($email)) {
|
|
$errors[] = 'Email is required';
|
|
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$errors[] = 'Invalid email format';
|
|
}
|
|
if (empty($password)) {
|
|
$errors[] = 'Password is required';
|
|
}
|
|
|
|
if (empty($errors)) {
|
|
try {
|
|
$pdo = db();
|
|
$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 already exists';
|
|
} else {
|
|
$password_hash = password_hash($password, PASSWORD_DEFAULT);
|
|
$insert_stmt = $pdo->prepare("INSERT INTO users (username, email, password_hash) VALUES (:username, :email, :password_hash)");
|
|
$insert_stmt->execute([
|
|
':username' => $username,
|
|
':email' => $email,
|
|
':password_hash' => $password_hash
|
|
]);
|
|
$_SESSION['user_id'] = $pdo->lastInsertId();
|
|
$_SESSION['username'] = $username;
|
|
$_SESSION['role'] = 'user';
|
|
header("Location: index.php");
|
|
exit();
|
|
}
|
|
} catch (PDOException $e) {
|
|
$errors[] = "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 - Car Sells in Afghanistan</title>
|
|
<link href="https://rsms.me/inter/inter.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
</head>
|
|
<body class="bg-slate-50 dark:bg-slate-900 text-slate-800 dark:text-slate-200">
|
|
|
|
<?php include 'partials/navbar.php'; ?>
|
|
|
|
<main class="container mx-auto px-4 py-8">
|
|
<div class="max-w-md mx-auto bg-white dark:bg-slate-800 rounded-lg shadow-md p-8">
|
|
<h1 class="text-2xl font-bold text-center mb-6">Create an Account</h1>
|
|
|
|
<?php if (!empty($errors)): ?>
|
|
<div class="bg-red-100 dark:bg-red-900 border border-red-400 text-red-700 dark:text-red-200 px-4 py-3 rounded relative mb-4" role="alert">
|
|
<ul>
|
|
<?php foreach ($errors as $error): ?>
|
|
<li><?php echo htmlspecialchars($error); ?></li>
|
|
<?php endforeach; ?>
|
|
</ul>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<form action="register.php" method="POST">
|
|
<div class="mb-4">
|
|
<label for="username" class="block text-sm font-medium mb-2">Username</label>
|
|
<input type="text" id="username" name="username" class="w-full px-3 py-2 bg-slate-100 dark:bg-slate-700 rounded-md focus:outline-none focus:ring-2 focus:ring-orange-500" required>
|
|
</div>
|
|
<div class="mb-4">
|
|
<label for="email" class="block text-sm font-medium mb-2">Email Address</label>
|
|
<input type="email" id="email" name="email" class="w-full px-3 py-2 bg-slate-100 dark:bg-slate-700 rounded-md focus:outline-none focus:ring-2 focus:ring-orange-500" required>
|
|
</div>
|
|
<div class="mb-6">
|
|
<label for="password" class="block text-sm font-medium mb-2">Password</label>
|
|
<input type="password" id="password" name="password" class="w-full px-3 py-2 bg-slate-100 dark:bg-slate-700 rounded-md focus:outline-none focus:ring-2 focus:ring-orange-500" required>
|
|
</div>
|
|
<button type="submit" class="w-full bg-orange-600 hover:bg-orange-700 text-white font-bold py-2 px-4 rounded-md transition duration-300">Register</button>
|
|
</form>
|
|
<p class="text-center text-sm mt-6">
|
|
Already have an account? <a href="login.php" class="text-orange-500 hover:underline">Login here</a>.
|
|
</p>
|
|
</div>
|
|
</main>
|
|
|
|
<?php include 'partials/footer.php'; ?>
|
|
|
|
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
|
|
</body>
|
|
</html>
|