PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Tasks: I have 2 login pages. main.php is the input window, then it post to check.php which checks whether login is successful and it is invisible to users! If login is ok, then redirects user to further page otherwise I want to redirect user to the main.php. But then I want to print a message "Login fails on main.php". How can I do this?
Set a session variable (before you redirect back to main.php) with the message, then get that message from the session in main.php (presumably clearing it afterwards so it doesn't print everytime you load main.php).
Pass the message as a parameter on the end of the redirect i.e.
Then get that message code in main.php and do what you want with it, i.e. if it's "1" you might display "Login failed, incorrect username", if it's "2" you might display "Login failed, incorrect password" or something similar.
Yes, SBro's first suggestion is the preferred way of achieving this if you want to avoid passing values around in the query string. Use functions though, or even better, create a Session class:
class Session {
function setFlash ($msg) {
$_SESSION['flashMsg'] = $msg;
}
function flash () {
echo $_SESSION['flashMsg'];
unset ($_SESSION['flashMsg']);
}
}
Use Session::setFlash('yourMessage') to set your login failure (or any other) message and then, after redirecting, use Session::flash() to output it. There you go