This is my Controller class login check section
case 'checkLogin':
$username = isset($_REQUEST['username']) ? trim($_REQUEST['username']) : "";
$password = isset($_REQUEST['password']) ? trim($_REQUEST['password']) : "";
try{
$login = $user->login($username,$password);
if ($login === false) {
throw new Exception("username or password is wrong");
}else {
$_SESSION['id'] = $login;
header('Location: index.php');
}
}
catch(Exception $ex){
$errMsg = $ex->getMessage();
$view->render('view/login.php', array('errMsg' => $errMsg ));
}
break;
UserModel Function
This is my user model function for checking username and password.
public function login($username,$password){
$username = strip_tags(stripslashes(mysql_real_escape_string($username)));
$password = strip_tags(stripslashes(mysql_real_escape_string($password)));
$stmt = $this->db->con->prepare("SELECT `password`, `id` FROM `user` WHERE `username` = ?");
$stmt->bindValue(1, $username);
try{
$stmt->execute();
$data = $stmt->fetch();
$stored_password = $data['password'];
$id = $data['id'];
if($stored_password === md5($password)){
return $id;
}else{
return false;
}
}catch(PDOException $e){
echo $e->getMessage();
}
}
Please tell me this is right.This code is working for me. I'm using this to implement basic MVC pattern login.
I've got some code from here http://www.sunnytuts.com/article/login-and-registration-with-object-oriented-php-and-pdo