more changes ig
This commit is contained in:
		@@ -10,7 +10,7 @@
 | 
				
			|||||||
    <h2>Login</h2>
 | 
					    <h2>Login</h2>
 | 
				
			||||||
    <form action="../login.php" method="post">
 | 
					    <form action="../login.php" method="post">
 | 
				
			||||||
        <input type="email" name="email" id="email-field" placeholder="E-Mail" required>
 | 
					        <input type="email" name="email" id="email-field" placeholder="E-Mail" required>
 | 
				
			||||||
        <input type="password" name="password" id="password-field" placeholder="Password" required>
 | 
					        <input type="password" name="password" id="password-field" pattern="\w{8,128}" placeholder="Password" required>
 | 
				
			||||||
        <input type="submit" value="Login">
 | 
					        <input type="submit" value="Login">
 | 
				
			||||||
    </form>
 | 
					    </form>
 | 
				
			||||||
</body>
 | 
					</body>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,9 +9,9 @@
 | 
				
			|||||||
<body>
 | 
					<body>
 | 
				
			||||||
    <h2>Register</h2>
 | 
					    <h2>Register</h2>
 | 
				
			||||||
    <form action="../register.php" method="post">
 | 
					    <form action="../register.php" method="post">
 | 
				
			||||||
        <input type="text" name="username" id="username-field" placeholder="Username" required>
 | 
					        <input type="text" name="username" id="username-field" pattern="\w{3,32}" placeholder="Username" required>
 | 
				
			||||||
        <input type="email" name="email" id="email-field" placeholder="E-Mail" required>
 | 
					        <input type="email" name="email" id="email-field" placeholder="E-Mail" required>
 | 
				
			||||||
        <input type="password" name="password" id="password-field" placeholder="Password" required>
 | 
					        <input type="password" name="password" id="password-field" pattern="\w{8,128}" placeholder="Password" required>
 | 
				
			||||||
        <input type="submit" value="Register">
 | 
					        <input type="submit" value="Register">
 | 
				
			||||||
    </form>
 | 
					    </form>
 | 
				
			||||||
</body>
 | 
					</body>
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										59
									
								
								register.php
									
									
									
									
									
								
							
							
						
						
									
										59
									
								
								register.php
									
									
									
									
									
								
							@@ -12,25 +12,50 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
 | 
				
			|||||||
        $email = $_POST['email'];
 | 
					        $email = $_POST['email'];
 | 
				
			||||||
        $password = $_POST['password'];
 | 
					        $password = $_POST['password'];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Hash the password
 | 
					        // Validate username length
 | 
				
			||||||
        $hashed_password = password_hash($password, PASSWORD_DEFAULT);
 | 
					        if (strlen($username) < 3 || strlen($username) > 32) {
 | 
				
			||||||
 | 
					            echo "Username must be between 3 and 32 characters.";
 | 
				
			||||||
        // Prepare and execute the SQL query using prepared statements
 | 
					        } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
 | 
				
			||||||
        $query = "INSERT INTO users (username, email, password) VALUES (?, ?, ?)";
 | 
					            // Validate email format
 | 
				
			||||||
        $stmt = $mysqli->prepare($query);
 | 
					            echo "Invalid email format.";
 | 
				
			||||||
        $stmt->bind_param("sss", $username, $email, $hashed_password);
 | 
					        } elseif (strlen($password) < 8 || strlen($password) > 128) {
 | 
				
			||||||
 | 
					            // Validate password length
 | 
				
			||||||
        // Execute the statement
 | 
					            echo "Password must be between 8 and 128 characters.";
 | 
				
			||||||
        $result = $stmt->execute();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if ($result) {
 | 
					 | 
				
			||||||
            echo "Registration successful. <a href='pages/login.html'>Login here</a>.";
 | 
					 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            echo "Error: " . $mysqli->error;
 | 
					            // Check if the username or email already exists
 | 
				
			||||||
        }
 | 
					            $checkQuery = "SELECT id FROM users WHERE username = ? OR email = ?";
 | 
				
			||||||
 | 
					            $checkStmt = $mysqli->prepare($checkQuery);
 | 
				
			||||||
 | 
					            $checkStmt->bind_param("ss", $username, $email);
 | 
				
			||||||
 | 
					            $checkStmt->execute();
 | 
				
			||||||
 | 
					            $checkStmt->store_result();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Close the statement
 | 
					            if ($checkStmt->num_rows > 0) {
 | 
				
			||||||
        $stmt->close();
 | 
					                echo "Username or email already exists. Please choose a different one.";
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                // Hash the password
 | 
				
			||||||
 | 
					                $hashed_password = password_hash($password, PASSWORD_DEFAULT);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                // Prepare and execute the SQL query using prepared statements
 | 
				
			||||||
 | 
					                $insertQuery = "INSERT INTO users (username, email, password) VALUES (?, ?, ?)";
 | 
				
			||||||
 | 
					                $insertStmt = $mysqli->prepare($insertQuery);
 | 
				
			||||||
 | 
					                $insertStmt->bind_param("sss", $username, $email, $hashed_password);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                // Execute the statement
 | 
				
			||||||
 | 
					                $result = $insertStmt->execute();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if ($result) {
 | 
				
			||||||
 | 
					                    echo "Registration successful. <a href='pages/login.html'>Login here</a>.";
 | 
				
			||||||
 | 
					                } else {
 | 
				
			||||||
 | 
					                    echo "Error: " . $mysqli->error;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                // Close the statements
 | 
				
			||||||
 | 
					                $insertStmt->close();
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // Close the statement for checking existing username or email
 | 
				
			||||||
 | 
					            $checkStmt->close();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
?>
 | 
					?>
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user