will35010 Posted May 4, 2010 Share Posted May 4, 2010 I cannot get this script to work properly. It logs the user in and redirects them even if the credentials are invalid. How do I fix it please? Thanks! <?php session_start(); include('db.php'); if (isset($_POST['username'])) { $username = $_POST['username']; $password = $_POST['password']; //$username = mysqli_real_escape_string($_POST['username']); //$password = mysqli_real_escape_string($_POST['password']); // MySQL Query $result = mysqli_query($conn, "SELECT * FROM staff WHERE username = '$username' AND password = '$password' "); if(!$result) { $_SESSION['error'] = '<span style="color: red">Login Failed</span>'; } else { // Mysql fetch row results $row = mysqli_fetch_assoc($result); $_SESSION['userid'] = $row['id']; $_SESSION['username'] = $username; $_SESSION['error'] = 'Login successful<br> Welcome, '.$username; header('Location: ./template.php'); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/200682-sessions-login-script-problem/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 4, 2010 Share Posted May 4, 2010 $result will only be false if the query fails due to an error (sql syntax, database problem...) If the query executes without error, even if there are zero matching rows in the result set, $result will contain a result resource and pass the tests in your code. You need to test the number of rows returned by the query. Quote Link to comment https://forums.phpfreaks.com/topic/200682-sessions-login-script-problem/#findComment-1053093 Share on other sites More sharing options...
satya61229 Posted May 4, 2010 Share Posted May 4, 2010 You need few things for redirect problem after users logout. I am in no mood of writing those long code written on blog. So search in the blog mentioned below. I am 100 % sure you will get the answer. Quote Link to comment https://forums.phpfreaks.com/topic/200682-sessions-login-script-problem/#findComment-1053094 Share on other sites More sharing options...
JAY6390 Posted May 4, 2010 Share Posted May 4, 2010 session_start(); include ('db.php'); if (isset($_POST['username'])) { $username = $_POST['username']; $password = $_POST['password']; //$username = mysqli_real_escape_string($_POST['username']); //$password = mysqli_real_escape_string($_POST['password']); // MySQL Query $result = mysqli_query($conn, "SELECT * FROM staff WHERE username = '$username' AND password = '$password' LIMIT 1 "); if (!$result || mysqli_num_rows($result) !== 1) { $_SESSION['error'] = '<span style="color: red">Login Failed</span>'; } else { // Mysql fetch row results $row = mysqli_fetch_assoc($result); $_SESSION['userid'] = $row['id']; $_SESSION['username'] = $username; $_SESSION['error'] = 'Login successful<br> Welcome, ' . $username; header('Location: ./template.php'); exit(); } } Try that Quote Link to comment https://forums.phpfreaks.com/topic/200682-sessions-login-script-problem/#findComment-1053106 Share on other sites More sharing options...
will35010 Posted May 4, 2010 Author Share Posted May 4, 2010 Thank you! I fixed it with this: <?php session_start(); include('db.php'); if (isset($_POST['username'])) { $username = $_POST['username']; $password = $_POST['password']; //$username = mysqli_real_escape_string($_POST['username']); //$password = mysqli_real_escape_string($_POST['password']); // MySQL Query $result = mysqli_query($conn, "SELECT * FROM staff WHERE username = '$username' AND password = '$password' "); // Mysql_num_row is counting table row $count=mysqli_num_rows($result); if($count==1){ // Mysql fetch row results $row = mysqli_fetch_assoc($result); $_SESSION['userid'] = $row['id']; $_SESSION['username'] = $username; $_SESSION['error'] = 'Login successful<br> Welcome, '.$username; header('Location: ./template.php'); } else { echo "Login Failed"; } } ?> $result will only be false if the query fails due to an error (sql syntax, database problem...) If the query executes without error, even if there are zero matching rows in the result set, $result will contain a result resource and pass the tests in your code. You need to test the number of rows returned by the query. Quote Link to comment https://forums.phpfreaks.com/topic/200682-sessions-login-script-problem/#findComment-1053110 Share on other sites More sharing options...
PFMaBiSmAd Posted May 4, 2010 Share Posted May 4, 2010 nevermind. Quote Link to comment https://forums.phpfreaks.com/topic/200682-sessions-login-script-problem/#findComment-1053112 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.