| | 8 | {{{#!sql |
| | 9 | <?php |
| | 10 | session_start(); |
| | 11 | |
| | 12 | if ($_SERVER["REQUEST_METHOD"] !== "POST") { |
| | 13 | header("Location: /Sign&Log.php"); |
| | 14 | exit(); |
| | 15 | } |
| | 16 | |
| | 17 | // Basic input validation |
| | 18 | if (empty($_POST['email'])) { |
| | 19 | header("Location: /Sign&Log.php?error=INVALID_EMAIL"); |
| | 20 | exit(); |
| | 21 | } |
| | 22 | if (empty($_POST['username'])) { |
| | 23 | header("Location: /Sign&Log.php?error=INVALID_USERNAME&email=" . urlencode($_POST['email'])); |
| | 24 | exit(); |
| | 25 | } |
| | 26 | if (empty($_POST["password"])) { |
| | 27 | header("Location: /Sign&Log.php?error=INVALID_PASSWORD&email=" . urlencode($_POST['email']) . "&username=" . urlencode($_POST['username'])); |
| | 28 | exit(); |
| | 29 | } |
| | 30 | |
| | 31 | $input_name = trim(htmlspecialchars($_POST['username'])); |
| | 32 | $input_email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL); |
| | 33 | $input_password = $_POST['password']; |
| | 34 | $role = 'Member'; |
| | 35 | |
| | 36 | // Additional validation |
| | 37 | if (!$input_email) { |
| | 38 | header("Location: /Sign&Log.php?error=INVALID_EMAIL"); |
| | 39 | exit(); |
| | 40 | } |
| | 41 | |
| | 42 | // Password strength validation |
| | 43 | if (strlen($input_password) < 8) { |
| | 44 | header("Location: /Sign&Log.php?error=WEAK_PASSWORD&email=" . urlencode($_POST['email']) . "&username=" . urlencode($input_name)); |
| | 45 | exit(); |
| | 46 | } |
| | 47 | |
| | 48 | // Username validation (alphanumeric and underscore only) |
| | 49 | if (!preg_match('/^[a-zA-Z0-9_]+$/', $input_name)) { |
| | 50 | header("Location: /Sign&Log.php?error=INVALID_USERNAME_FORMAT&email=" . urlencode($_POST['email'])); |
| | 51 | exit(); |
| | 52 | } |
| | 53 | |
| | 54 | try { |
| | 55 | require 'connect.php'; |
| | 56 | |
| | 57 | // Check for existing username |
| | 58 | $stmt = $conn->prepare("SELECT COUNT(*) FROM Users WHERE username = :username"); |
| | 59 | $stmt->bindParam(':username', $input_name, PDO::PARAM_STR); |
| | 60 | $stmt->execute(); |
| | 61 | |
| | 62 | if ($stmt->fetchColumn() > 0) { |
| | 63 | header("Location: /Sign&Log.php?error=USERNAME_TAKEN&email=" . urlencode($_POST['email'])); |
| | 64 | exit(); |
| | 65 | } |
| | 66 | |
| | 67 | // Check for existing email |
| | 68 | $stmt = $conn->prepare("SELECT COUNT(*) FROM Users WHERE email = :email"); |
| | 69 | $stmt->bindParam(':email', $input_email, PDO::PARAM_STR); |
| | 70 | $stmt->execute(); |
| | 71 | |
| | 72 | if ($stmt->fetchColumn() > 0) { |
| | 73 | header("Location: /Sign&Log.php?error=EMAIL_TAKEN&username=" . urlencode($input_name)); |
| | 74 | exit(); |
| | 75 | } |
| | 76 | |
| | 77 | // Hash password |
| | 78 | $hashed_password = password_hash($input_password, PASSWORD_ARGON2ID, [ |
| | 79 | 'memory_cost' => 65536, |
| | 80 | 'time_cost' => 4, |
| | 81 | 'threads' => 3 |
| | 82 | ]); |
| | 83 | |
| | 84 | // Insert new user |
| | 85 | $stmt = $conn->prepare(' |
| | 86 | INSERT INTO Users (username, email, password, role) |
| | 87 | VALUES (:username, :email, :password, :role) |
| | 88 | '); |
| | 89 | |
| | 90 | $stmt->bindParam(':username', $input_name, PDO::PARAM_STR); |
| | 91 | $stmt->bindParam(':email', $input_email, PDO::PARAM_STR); |
| | 92 | $stmt->bindParam(':password', $hashed_password, PDO::PARAM_STR); |
| | 93 | $stmt->bindParam(':role', $role, PDO::PARAM_STR); |
| | 94 | |
| | 95 | $stmt->execute(); |
| | 96 | |
| | 97 | session_regenerate_id(true); |
| | 98 | $_SESSION['username'] = $input_name; |
| | 99 | $_SESSION['userid'] = $conn->lastInsertId(); |
| | 100 | $_SESSION['role'] = $role; |
| | 101 | $_SESSION['last_activity'] = time(); |
| | 102 | $_SESSION['ip_address'] = $_SERVER['REMOTE_ADDR']; |
| | 103 | |
| | 104 | // Redirect to homepage after successful registration |
| | 105 | header("Location: ./HomePage.php"); |
| | 106 | exit(); |
| | 107 | |
| | 108 | } catch (PDOException $e) { |
| | 109 | error_log("Registration error: " . $e->getMessage()); |
| | 110 | header("Location: /Sign&Log.php?error=SERVER_ERROR"); |
| | 111 | exit(); |
| | 112 | } |
| | 113 | }}} |