115 lines
4.2 KiB
PHP
115 lines
4.2 KiB
PHP
<?php
|
|
ini_set('display_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
session_start();
|
|
|
|
if (isset($_SESSION['user_id'])) {
|
|
header('Location: dashboard.php');
|
|
exit;
|
|
}
|
|
|
|
$errors = [];
|
|
$success = '';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$email = trim($_POST['email']);
|
|
$password = $_POST['password'];
|
|
$password_confirm = $_POST['password_confirm'];
|
|
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$errors[] = 'Invalid email format.';
|
|
}
|
|
|
|
if (strlen($password) < 8) {
|
|
$errors[] = 'Password must be at least 8 characters long.';
|
|
}
|
|
|
|
if ($password !== $password_confirm) {
|
|
$errors[] = 'Passwords do not match.';
|
|
}
|
|
|
|
if (empty($errors)) {
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare('SELECT id FROM users WHERE email = ?');
|
|
$stmt->execute([$email]);
|
|
if ($stmt->fetch()) {
|
|
$errors[] = 'Email already registered.';
|
|
} else {
|
|
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
|
|
$stmt = $pdo->prepare('INSERT INTO users (email, password) VALUES (?, ?)');
|
|
if ($stmt->execute([$email, $hashed_password])) {
|
|
$success = 'Registration successful! You can now <a href="login.php">login</a>.';
|
|
} else {
|
|
$errors[] = 'Failed to register user. Please try again.';
|
|
}
|
|
}
|
|
} 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 - AI Cinema Engine</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>
|
|
|
|
<div class="container mt-5">
|
|
<div class="row justify-content-center">
|
|
<div class="col-md-6">
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h4>Register</h4>
|
|
</div>
|
|
<div class="card-body">
|
|
<?php if (!empty($errors)): ?>
|
|
<div class="alert alert-danger">
|
|
<?php foreach ($errors as $error): ?>
|
|
<p class="mb-0"><?php echo $error; ?></p>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
<?php if ($success): ?>
|
|
<div class="alert alert-success">
|
|
<p class="mb-0"><?php echo $success; ?></p>
|
|
</div>
|
|
<?php else: ?>
|
|
<form action="register.php" method="POST">
|
|
<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>
|
|
<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>
|
|
<button type="submit" class="btn btn-primary">Register</button>
|
|
</form>
|
|
<?php endif; ?>
|
|
</div>
|
|
<div class="card-footer text-center">
|
|
<a href="login.php">Already have an account? Login</a>
|
|
</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>
|