Page 1 of 1

redirecting back to page after die

Posted: Sun Apr 12, 2009 1:25 pm
by softraen
Hi this is the code, I am needing to return the user back to the login page after they have incorrectly put in the wrong password.


if(!$_POST['username'] | !$_POST['pass']) {
die('You did not fill in a required field.');
}

This is what I thought would work but it doesn't!

<a href="forum.php"><h1>Click here to try again</h1>

Any help would be great also if you could explain it a little? :)

Re: redirecting back to page after die

Posted: Sun Apr 12, 2009 2:22 pm
by kaszu

Code: Select all

if(empty($_POST['username']) || empty($_POST['pass'])) {
    header('Location: login.php');
    exit;
}

Re: redirecting back to page after die

Posted: Sun Apr 12, 2009 2:29 pm
by califdon
The die() function does just what you might expect: it KILLS THE SCRIPT. That means that NOTHING following its execution is carried out. I can't understand what you're trying to do. If you are trying to "return the user back to the login page after they have incorrectly put in the wrong password", that code surely won't do that. Your code only tests whether either of two form inputs are missing and if so, stops running immediately after sending a message to the browser. To do what you said you want to do, you would have to test the password against something (a database, or a file, or a hard-coded password in the script) and if it isn't the same, do a redirect, which could be done in a couple of ways, depending on whether or not your script has sent anything to the browser yet. You might read http://php.dzone.com/news/php-redirect-function. If you have already sent any output to the browser, you can send a short Javascript script snippet that either redirects or, often preferable, returns the browser to the page it was on previously (see http://www.javascriptkit.com/jsref/history.shtml).