Spaces:
Configuration error

lost-found-tracker-pro / register.php
brucelloyd1's picture
You are a professional full-stack web developer assistant.
8df07b6 verified
Raw History Blame Contribute Delete
4.6 kB
```php
<?php
include 'config/database.php';
include 'includes/auth.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'] ?? '';
$email = $_POST['email'] ?? '';
$password = $_POST['password'] ?? '';
$role = $_POST['role'] ?? 'finder';
$db = new Database();
$connection = $db->getConnection();
// Check if email already exists
$checkQuery = "SELECT * FROM users WHERE email = :email";
$checkStmt = $connection->prepare($checkQuery);
$checkStmt->bindParam(':email', $email);
$checkStmt->execute();
if ($checkStmt->rowCount() > 0) {
$error = "Email already registered";
} else {
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
$query = "INSERT INTO users (name, email, passwordHash, role) VALUES (:name, :email, :password, :role)";
$stmt = $connection->prepare($query);
$stmt->bindParam(':name', $name);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':password', $hashedPassword);
$stmt->bindParam(':role', $role);
if ($stmt->execute()) {
// Auto-login after registration
$userId = $connection->lastInsertId();
$_SESSION['user_id'] = $userId;
$_SESSION['user_name'] = $name;
$_SESSION['role'] = $role;
$_SESSION['email'] = $email;
header("Location: dashboard.php");
exit();
} else {
$error = "Registration failed. Please try again.";
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Register - Lost & Found</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<div class="container">
<nav>
<div class="logo">Lost & Found Pro</div>
<ul class="nav-links">
<li><a href="home.php">Home</a></li>
<li><a href="items.php">Browse Items</a></li>
<li><a href="login.php">Login</a></li>
<li><a href="register.php">Register</a></li>
</ul>
</nav>
</div>
</header>
<main class="container">
<div style="max-width: 400px; margin: 4rem auto;">
<div class="card">
<div class="card-header">
<h2 class="text-center">Register</h2>
</div>
<?php if (isset($error)): ?>
<div style="background: #FEE2E2; color: #DC2626; padding: 1rem; border-radius: 5px; margin-bottom: 1rem;">
<?php echo $error; ?>
</div>
<?php endif; ?>
<form method="POST">
<div class="form-group">
<label for="name">Full Name</label>
<input type="text" id="name" name="name" class="form-control" required>
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" id="email" name="email" class="form-control" required>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="password" name="password" class="form-control" required>
</div>
<div class="form-group">
<label for="role">I am a:</label>
<select id="role" name="role" class="form-control" required>
<option value="finder">Finder (I want to report found items)</option>
<option value="owner">Owner (I want to claim lost items)</option>
</select>
</div>
<button type="submit" class="btn btn-primary w-full">Register</button>
</form>
<p class="text-center mt-2">
Already have an account? <a href="login.php">Login here</a>
</p>
</div>
</div>
</main>
<footer>
<div class="container">
<div class="footer-content">
<div class="logo">Lost & Found Pro</div>
<p>&copy; 2024 Lost & Found Management System. All rights reserved.</p>
</div>
</div>
</footer>
</body>
</html>
```