【发布时间】:2014-01-01 13:23:02
【问题描述】:
我有一个带有用户名和密码的登录数据库,以及一个带有用户名和密码输入类型的 html 文件。
在我的课堂上登录:
class login
{
protected $_username;
protected $_password;
public function __construct($username, $password) //$username and $password values will be from the $_POST['username and password'] when the user submits the username and password
{
$this->username = $username;
$this->password = md5($password);
}
public function login()
{
try {
$pdo = dbConnect::getConnection();
$smt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
$smt->bindParam(1, $this->username);
$smt->execute();
echo "<pre>";
print_r($smt->fetch(PDO::FETCH_OBJ)); //testing if $smt will return results..yes it returned
if($smt->fetch()) {
print_r($smt->fetch(PDO::FETCH_OBJ)); //doesn't return ??this is my question... all other arguments inside this if loop is not executed..
while($row = $smt->fetch(PDO::FETCH_OBJ)) {
echo "one";
if($row->password == $this->password) {
header("Location: admin.php");
}
else {
echo '<div class="alert alert-error alert-block"><button type="button" class="close" data-dismiss="alert">×</button><h4>Error!</h4>username and password do not match!</div>';
}
}
}
else {
echo '<div class="alert alert-error alert-block"><button type="button" class="close" data-dismiss="alert">×</button><h4>Error!</h4>Username does not exist!</div>';
}
}
catch (PDOException $e) {
die($e->getMessage());
}
}
}
问题在于,在 PDO 中,它不会返回我在 if($smt->fetch()) 之后一直在请求的数据,该 if($smt->fetch()) 用于了解查询是否返回结果.. 在获取之前, print_r 返回数据...因此我无法继续我的代码..我是 OOP 和 PDO 的新手,这就是为什么我无法处理这个问题,这与 mysql 或 mysqli 函数不同..我是 PDO 的新手,我也使用 sqlite这里..
【问题讨论】:
-
很遗憾,这个网站不是为了修复你的代码。
-
为了您的用户,请不要使用未加盐的 MD5 进行密码散列。这是完全不安全的。在此处搜索 PHP 中的安全密码散列以获得首选替代方案。 This is a great place to start
-
Michael 打败了我,但我还是要说。
md5($password)不是一个好用的方法。它“太快了”,不再是用于密码的安全方法。