这是我的控制器类登录检查部分
案例“检查登录”:
$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;
用户模型函数
这是我用于检查用户名和密码的用户模型功能。
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();
}
}
请告诉我这是对的。这段代码对我有用。我正在使用它来实现基本的 MVC 模式登录。
我从这里得到了一些代码 http://www.sunnytuts.com/article/login-and-registration-with-object-orientation-php-and-pdo