How do I wait a certain amount of time before continuing in PHP?
So this is kind of two questions...
First off, when a user logs out, I want to redirect them back to the login page. I do this by using header('login_form.php');
, however, prior to doing such, I try to print out the text "You have successfully logged out. You are now being redirected to the login form.". I do this using echo and echoing out the text, however, the header runs so quickly that I'm almost immediately transferred back to the login form, without getting a chance to read the message. So, how can I delay a little bit, and let the message display for just 5 seconds or so prior to redirecting the user with the header?
My second question is, what's the best way to simply display "incorrect login" above the login form? So, when I browse to the login form 开发者_JAVA技巧and enter an incorrect user/password, instead of going to "login.php" and then redirecting back to login_form.php, it simply determines whether the login was a success or not, and if not simply displays "Incorrect Login" above the input fields, as opposed to having to redirect back to login_form (which also doesn't notify the user that the login was incorrect).
Return a simple HTML page with a meta refresh tag pointing to the desired location in the <head>
.
<meta http-equiv="refresh" content="5;url=login_form.php">
This will refresh (redirect) the page after 5 seconds to the given URL. I'd also consider adding a direct <a>
link for the impatient.
<p>
You are logged out. You will be redirected to the login page in 5 seconds.
Click <a href="login_form.php">here</a> if you're impatient.
</p>
I personally find 5 seconds too long. Make it 3.
As to your second question, just let the form submit to self and redisplay the very same form with dynamically inlined error messages next to the input fields. Only redirect if the login has succeeded.
Doing this in php alone would be difficult, or perhaps even impossible depending on the circumstances. Instead, take them to
logout.php
, or whatever page displays "You have been logged out," and simply add ameta
refresh
header, as in:<meta http-equiv="refresh" content="5; url=login_form.php">
This will redirect them tologin_form
in 5 seconds. This is a popular technique, but I can't guarantee it will work in all circumstances either. However, as the other answer suggests, you can include a link to redirect directly.This can be done with javascript (you can perform the validation with ajax and display an appropriate error message/let them log in as needed). If javascript is not an option or if you also need to handle users who don't have it, when
login.php
rejects the authentication, set a_SESSION
parameter such as_SESSION[login_fail_message] = true
. Onlogin_form.php
, check if this value is true. If it is, display your "Incorrect login" message. Clear this value on login attempt (or ideally if they leave the page).
For the first part, know that changing the location header will have an immediate effect. Some sites allow the login form to display the message that the user has successfully logged out.
For the second part, have javascript do the validation first and display any errors client-side. If it passes the javascript, have the server do the server-side validation from the POST and return the login form again, but this time with the error areas marked. A login success should redirect them instead as normal.
精彩评论