1

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>
Ginger and Lavender
  • 436
  • 1
  • 8
  • 21
  • 1
    `exit` will stop the PHP execution when called. It would depend on the rest of the script and what needs to occur. Also note that is JS, not jquery. You could use `header` instead of the JS redirect. – user3783243 Jun 18 '18 at 03:59
  • When the redirection occurs everything else has been finished. It is the final part of the success path of the script. – Ginger and Lavender Jun 18 '18 at 04:00
  • 1
    It won't add anything, and it possibly could break the DOM so I'd say no. – user3783243 Jun 18 '18 at 04:04
  • I asked this question because exit() seemed to prevent some of my jQuery animations from activating. Taking it out of the script fixed it, but I wanted to make sure it was okay to do this. – Ginger and Lavender Jun 18 '18 at 04:05
  • 1
    Yes, that is because anything after `exit()` won't be put into the DOM. – user3783243 Jun 18 '18 at 04:06
  • Possible duplicate of [php - Should I call exit() after calling Location: header?](https://stackoverflow.com/questions/3553698/php-should-i-call-exit-after-calling-location-header) – Ramlal S Jun 18 '18 at 04:12
  • 2
    @RamlalS No, this is not using `header`. This is using a redirect **after** the page loads. – user3783243 Jun 18 '18 at 04:13

2 Answers2

2

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>');
GROVER.
  • 4,071
  • 2
  • 19
  • 66
  • This is a very good idea, though the only issue it brings for me is that it doesn't allow me to clear my form values with jQuery. Everything else works, and clearing the form is purely for aesthetic value, but it would be nice to figure out how to get both to work together. – Ginger and Lavender Jun 18 '18 at 04:44
  • If you include how your form is formatted and _"cleared"_ I would be happy to help you out :) – GROVER. Jun 18 '18 at 04:45
  • To be clear, I have javascript inside script tags after the php code in the same file as the script. This does not run after die/exit is called. – Ginger and Lavender Jun 18 '18 at 04:47
  • It has been updated, hopefully with enough relevant code. – Ginger and Lavender Jun 18 '18 at 04:51
1

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.

Ramlal S
  • 1,573
  • 1
  • 14
  • 34