I wrote a function in PHP to check username and password from MySQL database and store as session after successful validation. But whatever value I enter to the input box it approves it and made a successful login. My Login code doesn’t work
here it is
function queryByUserAndPass($tableName, $username, $password){
$queryStatement = "SELECT * FROM ".$tableName." WHERE username='".$username."'
AND password='".$password."' LIMIT 1";
return $queryStatement;
}
function checkLogIn() {
if(isset($_POST['submit'])){
$username = $_POST['username'];
$password = $_POST['password'];
$queryState = queryByUserAndPass("nepal_users", $username, $password);
if( $resultQuery = mysql_query($queryState) ){
$found_user= mysql_fetch_array($resultQuery);
$_SESSION['id']=$found_user['id'];
$_SESSION['username']=$found_user['username'];
$message="succesful log in ".$_SESSION['username'];
header("location:home.php");
exit;
}else {
$message="error in log in";
}
}
}
Please tell me what is wrong in this code and why it is not working.
you dont check if username and password are correct!
IMPORTANT NOTE
The code above just illustrates where the logic problem in your code is. There are two other serious issues in it:
Its vulnerable to SQL injection. Using prepared statements is common practise. If prepared statemements are not availabe to you, use something like
$SAFE_USER_DATA = array_map('mysql_real_escape_string', $_POST);and use this instead of directly reading$_POSTdata. That way you ensure no malicious user can modify your SQL statements to gain access to your system.You should never store passwords as plain text – storing a salted SHA1 password hash is encouraged. do NOT simply use
md5($password). Attackers can easily decrypt those with the help of rainbow tables