0

My user registration PHP page for new user signup needs to show a confirmation message first, and then redirect to login page. For instance, after user successfully submits the registration form, I want to first show them a message that "You have registered successfully, redirecting you to login page", then let this message remain displayed for 5 seconds, and then redirect to my site's login PHP page.

I saw some existing SO questions like PHP redirect after 5 seconds and Page redirect after certain time PHP.

However, the problem in the solutions offered by both of them is that they use the header( "refresh:5;url=newpage.php" ); function which does NOT allow any message/output to be displayed BEFORE that header function call.

Is there a way to achieve the display of message first, then wait for X seconds, and then finally redirect to login page?

Aquaholic
  • 863
  • 9
  • 25
levent001
  • 174
  • 7
  • 1
    _"which does Not allow any messgae/output to be displayed before that header function call."_ - so output it _after_ the `header()` call then ... – CBroe Nov 22 '21 at 11:37
  • You mean echo/output statement at the top of the login.php file? That is also not possible, bexause that file too has a header function which will get killed with this echo at the top. – levent001 Nov 22 '21 at 11:41
  • No, in the same file where you call header. You send that header and some content back to the browser, so the browser will display that content for about five seconds, and then request the target page you want to redirect to. – CBroe Nov 22 '21 at 11:43
  • 1
    Does this answer your question? [How do I make a redirect in PHP?](https://stackoverflow.com/questions/768431/how-do-i-make-a-redirect-in-php). Especially take a look at Solutions part no. 6. – Markus Zeller Nov 22 '21 at 11:44
  • "Worst" case: create a file `mail_success.php` which has the `` header and shows the message. Use `header('Location: mail_success.php');` to call that page, which will then redirect after X seconds – brombeer Nov 22 '21 at 11:52
  • Markus - https://stackoverflow.com/a/32611589/6337701 answer cites problems with part no. 6 you mentioned – levent001 Nov 22 '21 at 11:54

2 Answers2

0

If you know i want to Redirect after a certain time, you can achieve that with HTML-Redirect as follows: (the content=5 means 5 seconds)

<meta http-equiv="refresh" content="5;url=https://www.example.com/login/" />

Otherwhise you will need to use JavaScript, and trigger it on your event, like the succesfull registration or so and then use something like as in the following example:

<!DOCTYPE html>
<button onclick="Redirect('https://www.example.com/login/')">test</button>
<script>
function Redirect(url) {
    setTimeout(function(){window.location.href=url;},5000);
}
</script>
Schlotter
  • 105
  • 1
  • 6
  • 1
    Not quite, it's `content="5;url=https://www.example.com/login/"` – brombeer Nov 22 '21 at 12:06
  • thx, i changed it – Schlotter Nov 22 '21 at 12:08
  • The meta one is a problem as per https://stackoverflow.com/a/32611589/6337701 answer. And in the second one, where is the message to be displayed? Am I missing something? – levent001 Nov 22 '21 at 12:51
  • So you got a Registration page, the User klick "Register" and here, the question is how do you accomplish your regisration with your form. Do you use a Basic HTML-Form to the page itself, to another page or do you use AJAX ? The way you display your message is based on your approach. – Schlotter Nov 22 '21 at 13:27
  • As mentioned in the q, hitting submit button on registration page (basic HTML-PHP) takes to the login page. And between the two, need to display the message for 5 seconds. – levent001 Nov 23 '21 at 06:06
  • Ok, ill post it as an new answer – Schlotter Nov 23 '21 at 09:42
0

In your JavaScript (LoginRedirect.js) File:

function LoginRedirect(id, url, s) {
    document.getElementById(id).style.display = "block";
    setTimeout(function(){window.location.href=url;}, s*1000);
}

And in your registration.php File:

$URL_Registration = 'https://www.example.com/registration.php';
if($user_creation_success === TRUE) {
    echo('
        <script src="LoginRedirect.js"></script>
        <div style="display:none;" id="login-success">
            <h1>Success</h1>
            <p>You have registered successfully, redirecting you to login page</p>
        </div>
        <script>LoginRedirect("login-success", "https://www.example.com/login/", 5);</script>');
} else {
    header("Location: $URL_Registration");
}

So this displays the "popup" on success and redirects after 5 seconds, otherwhise it redirects you straight back to your registration form.

Schlotter
  • 105
  • 1
  • 6
  • Hi @Schlotter, appreciate your continued help. But this too is Not waiting. It is directly and instantly taking to the login page, without showing any message. :-( – levent001 Nov 24 '21 at 15:12
  • 1
    Look at this example: https://jsfiddle.net/jvx8efbh/ and please make sure that the `$user_creation_success` variable is true on success. – Schlotter Nov 24 '21 at 15:47