Hi, i have this problem: I have a login form. If the user enters the correct username/password he gets redirected to the index.php page. If the user enters the incorrect username/password, he gets redirected back to the login.php page. The problem is that i can't echo a message when the user is redirected from the index back to the login page. Something like all the login forms on the internet have: "Username/password wrong. Please try again." Here is the code from my index.php page: <?php require_once("includes/functions.php"); session_start(); if ( (isset($_SESSION["username"])) && (isset($_SESSION["password"]))){ $username=$_SESSION['username']; if ($_SESSION['password']==get_password($username)){ // print the index specific info here echo "Sunteti logat ca userul :".$username." <br/>"; } else { echo "Username/password wrong. Please try again."; header( "Location: login.php" ); // cer o noua logare } } ?> PHP: The message "Username/password wrong. Please try again." is never displayed. Even is i type wrong username and password. Where should i put that echo message? Or is there something else wrong with the code?
Your problem is that you echo before your header. You can never send text to the browser before the header. If you want this page to display the error message instead of the login form you have two choices: Have this page display the login form again when there is an error. or Display your error, then do a javascript redirect or meta-refresh on the page. Either one of those will fix it for you, but whatever you decide to do you can never have an "echo" or any other output characters before a call to header.
try this and let me know what happens: <?php require_once("includes/functions.php"); session_start(); if ( (isset($_SESSION["username"])) && (isset($_SESSION["password"]))){ $username=$_SESSION['username']; if ($_SESSION['password']==get_password($username)){ // print the index specific info here echo "Sunteti logat ca userul :".$username." <br/>"; } else { ?> <meta http-equiv="REFRESH" content="5;url=http://yourdomain.com/login.php"> <?php echo "Username/password wrong. Please try again."; } } ?> PHP:
Your solution does work. BUT it not exactly what i wanted to do. Let me explain it more carefully: - if the user types the correct username/password combination, he will see the info from the index.php page - if the user types the wrong username/password combination, he will be redirected to the login.php page AND he will see an error message above the login form. (echo "Username and password are wrong.Please try again.";) It's just like any login form on the internet. If you know your username/password you'll get to see further info from you account. If not, you will have to enter the username/pass again and you'll see an error message. This is what i want to do.
How about you try this <?php require_once("includes/functions.php"); session_start(); if ( (isset($_SESSION['username'])) && (isset($_SESSION['password']))){ $username = $_SESSION['username']; $get_password = get_password($username) if ($_SESSION['password'], $get_password == $get_password) { ?> <meta http-equiv="REFRESH" content="5;url=http://yourdomain.com/index.php"> <?php echo "Thank you for logging in: $username. <br/>"; } else { ?> <meta http-equiv="REFRESH" content="5;url=http://yourdomain.com/login.php"> <?php echo "Username/password wrong. Please try again."; } } ?> PHP:
you could also try to redirect the user to the login page with a name/value parameter in the URL redirect. For example: if the user enters wrong details, you could redirect to login page (which i assume is index.php) http://mysite.com/index.php?error_code=1 ---------- In index.php (your login page) you could assign the respective error text (ex. see below): if ($_GET['error_code'] == 1) { echo ("OOPS, you entered the wrong username or password. Plz. try again"); } PHP:
use session variable to store error before redirect and display it on page where you are redirecting Hope that helps Regards Alex
just have the login form post to the same page where the form is and handle loggin in in that same php file. If correct, header redirect, else show the original page with an error message easy as stealing vegetables from a child ^^