I need help with my PHP registration form. I can't figure out what's wrong. Maybe you can see it. Data sent from my registration form are written to database even if submitted username or password is too short or long and when passwords don't match.
What to change in my code? I've been trying to correct it for hours.
Thanks in advance.
Registration form in index.php including <?php session_start(); ?>
above HTML
<form class="sign-up" action="users.php" method="post">
<p class="sign-up-title">Username:</p> <input class="sign-up-input" type="text" name="username" min="5" max="25">
<p class="sign-up-title">Password:</p> <input class="sign-up-input" type="password" name="pass" min="6" max="35">
<p class="sign-up-title">Confirm password:</p> <input class="sign-up-input" type="password" name="pass_check" min="6" max="35">
<p class="sign-up-title">E-mail:</p> <input class="sign-up-input" type="email" name="email">
<input id="sign-up-input-submit" class="sign-up-input" type="submit" value="Sign Up">
</form>
<?php
if (isset($_SESSION["username_error_short"])) {
echo $_SESSION["username_error_short"];
}
elseif (isset($_SESSION["username_error_long"])) {
echo $_SESSION["username_error_long"];
}
elseif (isset($_SESSION["username_error_exists"])) {
echo $_SESSION["username_error_exists"];
}
elseif (isset($_SESSION["pass_error_short"])) {
echo $_SESSION["pass_error_short"];
}
elseif (isset($_SESSION["pass_error_long"])) {
echo $_SESSION["pass_error_long"];
}
elseif (isset($_SESSION["pass_error_mismatch"])) {
echo $_SESSION["pass_error_mismatch"];
}
elseif (isset($_SESSION["email_error_exists"])) {
echo $_SESSION["email_error_exists"];
}
elseif (isset($_SESSION["registration_success"])) {
echo $_SESSION["registration_success"];
}
elseif (isset($_SESSION["registration_fail"])) {
echo $_SESSION["registration_fail"];
}
?>
Script in users.php
<?php
session_start();
include "connect.php";
global $db;
if (isset($_POST["username"]) || isset($_POST["pass"]) || isset($_POST["pass_check"])
|| isset($_POST["email"])) {
$username = $_POST["username"];
$password = $_POST["pass"];
$password_check = $_POST["pass_check"];
$email = $_POST["email"];
// check if username is too short/long
if (strlen($username) < 5) {
$_SESSION["username_error_short"] = "Username too short. Username should contain at least 5 characters.";
}
elseif (strlen($username) > 25) {
$_SESSION["username_error_long"] = "Username too long. Username should contain max. 25 characters.";
}
// check if username already exists in DB
elseif (strlen($password) >= 5 || strlen($password) <= 25) {
$sql_User_Duplicate = $db->prepare('SELECT * FROM users WHERE username = :username');
$sql_User_Duplicate->bindParam(':username', $username);
$sql_User_Duplicate->execute();
if ($sql_User_Duplicate->rowCount() > 0) {
$_SESSION["username_error_exists"] = "This username already exists. Select another one.";
}
else {
$usernameCheck = 1;
}
}
// check if password is too short/long
if (strlen($password) < 6) {
$_SESSION["pass_error_short"] = "Password too short. Password should contain at least 6 characters.";
$passwordCheck_length = 0;
}
elseif (strlen($password) > 35) {
$_SESSION["pass_error_long"] = "Password too long. Password should contain max. 35 characters.";
$passwordCheck_length = 0;
}
// check if $password matches $password_check
elseif (strlen($password) >= 6 || strlen($password) <= 35) {
if ($password == $password_check) {
$passwordCheck = 1;
}
else {
$_SESSION["pass_error_mismatch"] = "Passwords don't match. Try again.";
}
}
// check if email already exists in DB
$sql_Email_Duplicate = $db->prepare('SELECT * FROM users WHERE email = :email');
$sql_Email_Duplicate->bindParam(':email', $email);
$sql_Email_Duplicate->execute();
if ($sql_Email_Duplicate->rowCount() > 0) {
$_SESSION["email_error_exists"] = "This e-mail is already registered.";
}
else {
$emailCheck = 1;
}
// create new account
if ($usernameCheck == 1 || $passwordCheck == 1 || $emailCheck == 1) {
$sql_Account_Create = $db->prepare('INSERT INTO users (username, password, email) VALUES (:username, :password, :email)');
$sql_Account_Create->execute(array(":username" => $username, ":password" => $password, ":email" => $email));
// check if account (username) has been created in DB
$sql_Account_Create_Check = $db->prepare('SELECT * FROM users WHERE username = :username');
$sql_Account_Create_Check->bindParam(':username', $username);
$sql_Account_Create_Check->execute();
if ($sql_Account_Create_Check->rowCount() > 0) {
$_SESSION["registration_success"] = "Account registered successfully.";
}
else {
$_SESSION["registration_fail"] = "Something went wrong. Please check submitted data and try again later.";
}
}
}
header('Location: index.php');
?>