naveendk.55 Posted October 29, 2011 Share Posted October 29, 2011 Hi, I am trying to perform a login check and redirect the form to index.php if the username and password are correct. Also the login page check if the fields are entered or not. The issue is that the login check is performing but not redirecting to index.php. Also I'm not getting any error after performing the login check. Let me know if I'm making any errors in below code. the include file only contains the database details. <?php include("connections.php"); ?> <?php if(isset($_POST['submit'])) { $errors=array(); $required_fields=array('username', 'pass'); foreach($required_fields as $fields) { if(!isset($_POST[$fields]) || empty($_POST[$fields])) { $errors[]=$fields; } } if(empty($errors)) { $username=trim($_POST['username']); $password =trim($_POST['pass']); $query= "SELECT * FROM users WHERE username='{$username}' AND hashed_password='{$password}' "; $result=mysql_query($query); $result_set=mysql_fetch_array($result); if(mysql_num_rows($result_set) == 1) { header('location: index.php'); exit; } else { echo 'Invalid password'; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/250056-login-check/ Share on other sites More sharing options...
ZulfadlyAshBurn Posted October 29, 2011 Share Posted October 29, 2011 Enable php to display errors. There might be errors for running headers Quote Link to comment https://forums.phpfreaks.com/topic/250056-login-check/#findComment-1283281 Share on other sites More sharing options...
AyKay47 Posted October 29, 2011 Share Posted October 29, 2011 your error lies within theses lines.. $result=mysql_query($query); $result_set=mysql_fetch_array($result); if(mysql_num_rows($result_set) == 1) you are attempting to use the mysql_num_rows() on a mysql_fetch_array() function, this is invalid... change the lines to this, $result=mysql_query($query); $result_set=mysql_fetch_array($result); if(mysql_num_rows($result) == 1) Quote Link to comment https://forums.phpfreaks.com/topic/250056-login-check/#findComment-1283285 Share on other sites More sharing options...
naveendk.55 Posted October 31, 2011 Author Share Posted October 31, 2011 I did that and it still didn't work. Quote Link to comment https://forums.phpfreaks.com/topic/250056-login-check/#findComment-1283641 Share on other sites More sharing options...
seany123 Posted October 31, 2011 Share Posted October 31, 2011 try just echoing out: echo mysql_num_rows($result); and see if its actually found the row. try echoing out $username and $password to make sure its whats expected. i cant see where you have hashed $password before comparing it against hashed_password. Quote Link to comment https://forums.phpfreaks.com/topic/250056-login-check/#findComment-1283659 Share on other sites More sharing options...
Pikachu2000 Posted October 31, 2011 Share Posted October 31, 2011 Judging by the fact that your password field in the table is named hashed_password, I'm going to assume it actually contains a hashed password. You aren't hashing the plaintext password to compare against it, so if it is indeed hashed in the table, it will never match. Also, it's inefficient to to run a SELECT * query when all you need to know is the number of records that match. A COUNT() query would be better here. Since you expect exactly one record to match, check for that condition as well as conditions that are not the expected result, and handle them accordingly. $query = "SELECT COUNT(1) FROM users WHERE username='{$username}' AND hashed_password='{$password}' "; $result = mysql_query($query); $array = mysql_fetch_row($result); if( $array[0] == 1 ) { header('location: index.php'); exit; } elseif( $array[0] == 0 ) { // There was no matching record, so handle that condition. } else { // There was more than 1 matching record, indicating data corruption. This should be a fatal error condition, and should stop further processing. } Quote Link to comment https://forums.phpfreaks.com/topic/250056-login-check/#findComment-1283665 Share on other sites More sharing options...
cunoodle2 Posted October 31, 2011 Share Posted October 31, 2011 I wish I could press a "LIKE button" for Pikachu2000 responses. Quote Link to comment https://forums.phpfreaks.com/topic/250056-login-check/#findComment-1283673 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.