📄 Source: register.php
<?php
require_once 'includes/config.php';
require_once 'includes/db.php';
require_once 'includes/auth.php';
require_once 'includes/functions.php';
$error = '';
$success = '';
if (isLoggedIn()) {
header('Location: index.php');
exit();
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = sanitize($_POST['username'] ?? '');
$password = $_POST['password'] ?? '';
$confirmPassword = $_POST['confirm_password'] ?? '';
// Validate username - only letters, numbers, underscores, and hyphens
if (empty($username) || empty($password)) {
$error = 'Please fill in all fields';
} elseif (strlen($username) < 3) {
$error = 'Username must be at least 3 characters';
} elseif (strlen($username) > 30) {
$error = 'Username must be less than 30 characters';
} elseif (!preg_match('/^[a-zA-Z0-9_\-]+$/', $username)) {
$error = 'Username can only contain letters, numbers, underscores, and hyphens';
} elseif (strlen($password) < 6) {
$error = 'Password must be at least 6 characters';
} elseif ($password !== $confirmPassword) {
$error = 'Passwords do not match';
} elseif (getUserByUsername($username)) {
$error = 'Username already taken';
} else {
$userId = createUser($username, $password);
if ($userId) {
loginUser($userId);
header('Location: index.php?success=registered');
exit();
} else {
$error = 'Failed to create account';
}
}
}
?>
← Back