对于登录系统,我想检查用户名和密码是否正确,这是代码(没有检查错误等):
$db = @new MySQLi('localhost', 'user', 'pw', 'db');
$sql = 'SELECT
id
FROM
members
WHERE
username = ? AND
password = ?';
$stmt = $db->prepare($sql);
$stmt->bind_param('ss', $username, $password_hash);
$stmt->execute()
$stmt->bind_result($UserID);
现在我想检查是否有结果,在大多数教程中它都是这样做的:
if (!$stmt->fetch()) {
//LOGIN WAS INCORRECT, DO STUFF
} else {
//LOGIN WAS CORRECT, DO STUFF
}
但为什么呢?它也是这样的:
if (isset($UserID)){
//LOGIN WAS CORRECT, DO STUFF
} else {
//LOGIN WAS INCORRECT, DO STUFF
或:
if ($UserID != 0){
//LOGIN WAS CORRECT, DO STUFF
} else {
//LOGIN WAS INCORRECT, DO STUFF
那么最快,最安全的方法是什么?
答案 0 :(得分:0)
你不需要也不需要“最快”(因为没有什么是慢的)也不是“最安全的”(因为没有什么是不安全的)。
您需要的只是明智的方式。
$stmt = $db->prepare($sql);
$stmt->bind_param('ss', $username, $password_hash);
$stmt->execute()
$stmt->bind_result($UserID);
$stmt->fetch();
现在您要检查$UserID
变量并将其保存到会话中。
if ($UserID) {
$_SESSION['auth'] = $UserID;
}
正如你所看到的,原始的mysqli非常冗长和丑陋。这就是为什么你不应该使用它,而是像这样的一些抽象层
$db = new safeMysql();
$sql = 'SELECT id FROM members WHERE username = ?s AND password = ?s';
if ($UserID = $db->getOne($sql, $username, $password_hash));
$_SESSION['auth'] = $UserID;
}