ronlaboa Posted December 1, 2016 Share Posted December 1, 2016 hello I am new to php and know a little HTML & CSS I have copied some code from a tutorial just to test a home server I have setup. the code was working and some of it still does but a part of it doesnt work and leaves me with a blank screen. the part that doesnt seem to work is the else part of the statement. the code is for a very basic login page. here is the HTML for my index page <!DOCTYPE html> <head> <title>Login Page</title> </head> <body> <!-- Output error message if any --> <?php echo $error; ?> <!-- form for login --> <form method="post" action="/php/login.php"> <label for="username">Username:</label><br/> <input type="text" name="username" id="username"><br/> <label for="password">Password:</label><br/> <input type="password" name="password" id="password"><br/> <input type="submit" value="Log In!"> </form> </body> </html> and the code for my php login page <?php // Start the session session_start(); // Defines username and password. Retrieve however you like, $username = "user"; $password = "password"; // Error message $error = ""; // Checks to see if the user is already logged in. If so, refirect to correct page. if (isset($_SESSION['loggedIn']) && $_SESSION['loggedIn'] == true) { $error = "success"; header('Location: success.php'); } // Checks to see if the username and password have been entered. // If so and are equal to the username and password defined above, log them in. if (isset($_POST['username']) && isset($_POST['password'])) { if ($_POST['username'] == $username && $_POST['password'] == $password) { $_SESSION['loggedIn'] = true; header('Location: success.php'); } else { $_SESSION['loggedIn'] = false; $error = "Invalid username and password!"; } } ?> the login page works when I type the correct user name and password and takes me to the success page/php file which is <?php // Start the session ob_start(); session_start(); // Check to see if actually logged in. If not, redirect to login page if (!isset($_SESSION['loggedIn']) || $_SESSION['loggedIn'] == false) { header("Location: index.html"); } ?> <h1>Logged In!</h1> <form method="post" action="logout.php"> <input type="submit" value="Logout"> </form> but if i enter and incorrect username/password it redirects me to the login.php file but its blank. it did work at first but now nothing. as i said i am very new to php so any help would be very much appreciated thanks matthew Quote Link to comment https://forums.phpfreaks.com/topic/302658-php-code-stopped-working/ Share on other sites More sharing options...
Solution benanamen Posted December 1, 2016 Solution Share Posted December 1, 2016 (edited) Of course it's blank. All your doing is setting $error. The script is done by the time you get to this point. Think this through, I am sure you can figure out what needs to be changed. } else { $_SESSION['loggedIn'] = false; $error = "Invalid username and password!"; } FYI: This is no kind of logging in code you should be using. Edited December 1, 2016 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/302658-php-code-stopped-working/#findComment-1539937 Share on other sites More sharing options...
ronlaboa Posted December 1, 2016 Author Share Posted December 1, 2016 (edited) ah right ok so i need to display msg stating invalid login and link back to my index page thanks i think thats right anyway, the login.php and index.html were orignally one file called index.php I just tried to separate them to try to link to external php file from my html file Edited December 1, 2016 by ronlaboa Quote Link to comment https://forums.phpfreaks.com/topic/302658-php-code-stopped-working/#findComment-1539938 Share on other sites More sharing options...
benanamen Posted December 1, 2016 Share Posted December 1, 2016 (edited) i think thats right anyway, Well, that's not what I was getting at. The error check should come first, not last. Logically, a login error will always happen before a valid login, and a valid login will not happen before a login error. Edited December 1, 2016 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/302658-php-code-stopped-working/#findComment-1539942 Share on other sites More sharing options...
ronlaboa Posted December 1, 2016 Author Share Posted December 1, 2016 so you mean if username and pword dont match should be first conditional ? Quote Link to comment https://forums.phpfreaks.com/topic/302658-php-code-stopped-working/#findComment-1539943 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.