Log in

View Full Version : Help with DIE and EXIT please!?



mloclam
09-06-2014, 07:29 AM
Hi,

First post here, so want to say hello to everyone.

I know this is bad form, first post asking a question, but I thought I may as well get down to it... :)

I am quite new to PHP andhave a query I hope someone can help me resolve.

I am coding a Change Password routine and am having differing results with error handling. Basically, if a user enters an incorrect password, when I run the following code:



{
header("Location: change_password.php");
exit("New Passwords do not match.");
}

nothing really happens except the three "password" textboxes get cleared and the process starts again. This is fine, but there is no feedback to the user telling them they have entered an incorrect stored password. Similar happens when they enter new passwords that do not match.

So, to try to give them some feedback, I tried this code instead:



{
print '<script type="text/javascript">';
print 'alert("The new Passwords entered do not match.")';
print '</script>';
header("Location: change_password.php");
exit("New Passwords do not match.");
}

what happens now is they get an alert box informing them of their error, but once I click "Ok" the program stops altogether and the screen clears, then the error description in the exit statement is printed.

Can anyone help please!

Thanks :)

jscheuer1
09-06-2014, 02:40 PM
Oh yeah, this is not a hard and fast rule. But you almost never can use a header after you have written to the page. You could use javascript to change the location of the page:


{
print '<script type="text/javascript">';
print 'alert("The new Passwords entered do not match.");';
print 'window.location.replace("change_password.php");';
print '</script>';
print '<noscript>';
print 'The new Passwords entered do not match.<br>';
print '<a href="change_password.php">Try again</a>';
print '</noscript>';
exit("New Passwords do not match.");
}

There could be a better way. How did we get here? In PHP, it generally wouldn't know until you submitted the page. But javascript could check this and some other things before submitting.

hanny
09-30-2014, 09:52 AM
@mloclam you can't use header after print command.

jscheuer1
10-01-2014, 02:38 AM
This is getting to be an older thread, so I'm closing it. If anyone wants to continue the discussion, feel free to start a new thread and refer back to this one.