redirecting back to page after die

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
softraen
Forum Newbie
Posts: 1
Joined: Sun Apr 12, 2009 1:19 pm

redirecting back to page after die

Post 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? :)
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: redirecting back to page after die

Post by kaszu »

Code: Select all

if(empty($_POST['username']) || empty($_POST['pass'])) {
    header('Location: login.php');
    exit;
}
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: redirecting back to page after die

Post 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).
Post Reply