I have figured out a workaround to this problem using Javascript, but would like to know why this is happening, and to figure out a possible solution using PHP.
On my registration page (register.php) I use jQuery's preventDefault()
on the submit button, but if I call header("Location: /index.php");
upon successful registration, my index page is loaded on top of my register page. The URL in my browser still says register.php as well.
Redirecting with Javascript solves the problem, but why is this happening with PHP? All other functionality of my registration script works perfectly, including other places where, upon error, I use header()
to redirect users to my home page (i.e. when users try to navigate directly to .../_registerAccount.php);
jQuery:
/// <reference path="jquery-3.3.1.min.js" />
$(document).ready(function() {
$("form").submit(function(event) {
event.preventDefault();
var username = $("#register-username").val();
var email = $("#register-email").val();
var password = $("#register-password").val();
var confirmPassword = $("#register-confirm-password").val();
var submit = $("#register-submit").val();
$(".form-message").load("../shared/_registerAccount.php", {
username: username,
email: email,
password: password,
confirmPassword: confirmPassword,
submit: submit
});
});
});
PHP:
else
{
$errorEmpty = $errorUsername = $errorEmail = $errorPassword = $errorConfirmPassword = false;
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
mysqli_stmt_bind_param($statement, "sss", $username, $email, $hashedPassword);
mysqli_stmt_execute($statement);
session_start();
$_SESSION['register-success'] = 'You have successfully registered! Please verify your email before logging in.';
$registrationSuccessful = true;
//My Javascript workaround
//exit('<script type="text/javascript">location.assign("../index.php")</script><a href="../index.php">Home</a>');
header('Location: ../index.php');
exit();
}