I have some code to log how many password attempts have been tried that looks like this:
<开发者_StackOverflow;?php
/* Access denied */
$_SESSION['error'] = "invalidlogin";
if (isset($_SESSION['tries']))
{
if ($_SESSION['tries'] == 5)
{
//TODO: Redirect somewhere nicer and more descriptive
header("location:/index.php");
}
else
{
$_SESSION['tries']++;
}
}
else
{
$_SESSION['tries'] = 0;
}
header("location:/login.php");
?>
I know it is counting up to five and then stopping cuz I tested it by echoing $_SESSION['error'] on login.php
PHP never redirects when it hits
header("location:/index.php");
But it always keeps going and then redirects to
header("location:/login.php");
How can I get PHP to redirect as soon as it hits the index.php redirect?
Do I just set a boolean after the index.php redirect and check it on the login.php redirect?
EDIT: To clarify, I have already defined a session_start(); don't worry about that.
header("location:/index.php");
exit();
A redirect only takes effect AFTER it's been sent to the client's browser and the browser initiates a new request (which terminates the current request). Simply calling header() doesn't initiate the redirect - your script will keep running until it exits normally, the connection is closed and the webserver shuts down the script, or you explicitly terminate execution.
if you want a redirect header to take effect immediately, you need to terminate the script, which causes a flush of any pending output (including the redirect header):
header("Location: ...");
exit();
Per the PHP Docs it's good practice to call exit();
after calling header();
First of all, your entire method is flawed. You should not do a redirect upon login failure, that might cause security issues. You should see to it that your script includes the proper sub-page within the same request.
Although, technically the answer to your question is, as stated, to use exit().
u Forgot to start the session !!!! use exit or die after header("location:/index.php"); to do not continue the rest
精彩评论