Ginger and Lavender
Ginger and Lavender

Reputation: 468

Do I need to use exit() after redirecting my user off the page on which the script is executed?

After successful registration, my user is redirected to the home page using:

echo
    "<script type=\"text/javascript\">
        window.location.href='../index.php';
    </script>";

Do I need to follow this with

exit();

or is redirecting the user enough?

[EDIT] to help solve how to use die()/exit() alongside javascript in the same php file

jQuery:

$(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

        });
    });
});

end of PHP script (success path):

        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;

            die('<script type="text/javascript">location.assign("../index.php")</script><a href="../index.php">Home</a>');
        }

jQuery that follows PHP script in same file:

<script type="text/javascript">

    var registrationSuccessful = "<?php echo $registrationSuccessful; ?>";

    if (registrationSuccessful)
    {
        $("#register-username, #register-email, #register-password, 
            #register-confirm-password").val("");
    }

</script>

Upvotes: 1

Views: 186

Answers (2)

GROVER.
GROVER.

Reputation: 4378

You're assuming that JavaScript will be always available (or enabled) on all users browsers. I would heavily suggest utilising the native header PHP function:

die(header("Location: ../index.php"));

Or, if that's not a possibility (for whatever reason), at least give users an alternate way if JavaScript isn't enabled:

die('<script type="text/javascript">location.assign("../index.php")</script><a href="../index.php">Home</a>');

Upvotes: 2

Ramlal S
Ramlal S

Reputation: 1643

I recommend exit() ,If there is any further code after redirection.If you don't have any code then you don't have to. if you are using any alert before exit then stay page using refresh otherwise alert message in fraction of sec.

Upvotes: 1

Related Questions