I am creating a registration system for my website. When a new account is created the user should see a message that says "Successfully created a new account" and if the account cannot be created the user should see a message that says "Could not create a new account".

I made it so that if the user doesn't press the submit form button on index.php and they try to type

websitename.com/verify_registration_form.php

in the search bar, they are redirected to index.php

My problem is that the user is redirected to index.php regardless if they the pressed the submit form button or not.

Here is the registration form on index.php

<form action="verify_registration_form.php" method="post">


<input type="username" id="user_name" name="user_name placeholder="Username" required>


<input type="password" id="user_pass_word" name="user_pass_word" placeholder="Password" required>


<input type="email" id="user_email" name="user_email" placeholder="Email" required>


<input type="submit" id="submit_registration_form_button" name="submit_registration_form_button" value="Sign Up">


</form>

Here is the form verification on my verify_registration_form.php file

<?php

session_start();

if(isset($_POST["submit_registration_form_button")) {

$connection = mysqli_connect("localhost", "root", "", 
"websiteusers");

if(!$connection) {

echo "Could not connect to MYSQL database";

}else{

$connection = mysqli_connect("localhost", "root", "", "websiteusers");
$username = mysqli_real_escape_string($connection, $_POST["user_name"]);
$userpassword = mysqli_real_escape_string($connection, $_POST["user_pass_word");
$hasheduserpassword = password_hash($password, PASSWORD_DEFAULT);
$useremail = mysqli_real_escape_string($connection, $_POST["user_email"]);
$sql = "SELECT UserName FROM websiteusers WHERE UserName='$username' OR UserEmail='$useremail'";
$result = mysqli_query($connection, $sql);
$user = mysqli_fetch_assoc($result);
$errors = array();

if($user) {

if($user["UserName"] === $username) {

array_push($errors, "That username is already taken. Please choose another one.");

}

if($user["UserEmail"] === $useremail) {

array_push($errors, "That email address is already taken. Please choose another email address.");

}

if(count($errors) == 0) {

$user_name = $username;
$user_pass_word = $hasheduserpassword;
$user_email = $useremail;

$query = "INSERT INTO websitusers (UserName, UserPassWord, UserEmail) 
VALUES ('$user_name', '$user_pass_word', '$user_email')";
$connection = mysqli_connect("localhost", "root", "", "websiteusers");

if(mysqli_query($connection, $query)) {

echo "Successfully created a new account";

$_SESSION['username'] = $user_name;
$_SESSION['success'] = "Registration was a success";

}else{

echo "Could not create a new account";

}

}

}


}


}else{

header("Location: index.php");

}

?>

1 Answers

1
Marakusa On Best Solutions

This checks if user has posted the request:

    if($_SERVER['REQUEST_METHOD'] != 'POST')
    {
        //user didn't press the register button
    }
    else
    {
        //user pressed the register button
    }