我在使用PDO函数正确连接MySQL服务器时遇到了一些问题.我不能从数据库中查询我所需要的东西,我也不太确定该使用什么PDO函数.我GET 0作为结果.我希望验证我通过数据库输入的密码和用户名,并做一个if语句,如果信息正确,则启动会话。
这是我的UPDATED代码。
<?php
// if a value is given
if (isset($_POST['username' && 'password'));
{
// starts the session created if login info is correct
session_start();
// connectivity to MySQL server
$db = new PDO('mysql:host=host;dbname=name', 'username', 'password');
// information entered in form made into a variable
$username = PDO::quote($_POST['username']);
$password = PDO::quote($_POST['password']);
// after pressing login, checking if the variables exist in the database
if ($_POST['button'] == 'Login')
{
$query = $db->query("SELECT COUNT(*) FROM login WHERE username=:username AND password=:password");
$query->bindValue(':username', $username, PDO::PARAM_STR);
$query->bindValue(':password', $password, PDO::PARAM_STR);
$query->execute();
// Check the number of rows that match the SELECT statement
if($query = $db->fetch(PDO::FETCH_OBJ))
{
echo "No records found";
}
else
{
header("Location: members.php");
$_SESSION['username'] = $_POST['username'];
}
}
// After pressing register, stores variable and adds it in register field
else if ($_POST['register'] == 'Register')
{
header("Location: register.php");
$_SESSION['usernamereg'] = $_POST['username'];
}
// If no button is pressed, send to index
else
{
header("Location: http://www.gjertgjersund.com");
}
// closes the if value is given statement
}
?>
<!DOCTYPE html>
<html>
<head>
<title> Folder </title>
<link rel="stylesheet" type="text/css" href="frontpage.css">
</head>
<body>
<div id="box">
<div id="wrap">
<center>
<img src="./img/logo.png" alt="logo">
<form action='index.php' method='POST' autocomplete='off'>
<div class="usernameform">
<input type='text' name='username' style='border: none; font-size: 20px;'>
</div>
<br />
<div class="passwordform">
<input type='password' name='password' style='border: none; font-size: 20px;'>
</div>
<br />
<div class="registerlogin">
<input type='submit' name='button' value='Login' class='input'>
<input type='submit' name='register' value='Register' class='inputtwo'>
</div>
</form>
</center>
</div>
</div>
</body>
</html>
你不应该使用 '
在准备好的声明和 $username
是字符串而非整数
$query = $db->query("SELECT * FROM login WHERE username=:username AND password=:password");
$query->bindValue(':username', $username, PDO::PARAM_STR);
$query->bindValue(':password', $password, PDO::PARAM_STR);
$query->execute();
$row_count = $query->rowCount();
//{ remove it
您的 if
误区
// if a value is given
if (isset($_POST['username' && 'password'));
{
到
// if a value is given
if (isset($_POST['username']) and isset($_POST['password']))
{
有些数据库可能会返回SELECT语句返回的行数。然而,这种行为并不能保证所有的数据库都是如此,不应依赖见 指南. 您可以使用 COUNT(*)
和 fetchColumn()
如下面的查询,以模拟 rowCount()
.
$query = $db->query("SELECT COUNT(*) FROM login WHERE username=:username AND password=:password");
$query->bindValue(':username', $username, PDO::PARAM_STR);
$query->bindValue(':password', $password, PDO::PARAM_STR);
$query->execute();
// Check the number of rows that match the SELECT statement
if($query->fetchColumn() == 0) {
echo "No records found";
}else{
//CODE FOR Success
//Etc
}
这是一个非常好的问题,部分原因是它表明了很多不好的做法,可悲的是,这些做法一直存在到今天,距离这个问题发布已经快十年了。让我们把它们整理出来
我在使用PDO函数正确连接MySQL服务器时遇到了一些问题。
这是个很公平的问题,现在哪里的连接都只是一行代码。М任何重要的选项都要设置,看到一个 范例 我写的。你可以将连接代码存储在一个单独的文件中,然后将其包含在你的脚本中即可。
你永远不应该在数据库中存储明文密码。相反,密码必须是 散列使用专门的功能------------------------------。password_hash().
然后,为了验证密码,你已经去用 password_verify()
在您的密码被正确地哈希之前,请不要继续前进。请注意,哈希密码的字段大小必须是60个字符长。
终于,现在我们可以写代码了。可以简单得多,就几行。实际上,只需要一个条件。我们不需要提供任何细节来说明是登录还是用户名未找到--只需要 "登录和密码不匹配"。所以,这里是这样的。
<?php
if (isset($_POST['username']));
{
// get the PDO instance
include 'pdo.php';
// getting the record for the given username
$stmt = $pdo->prepare("SELECT * FROM login WHERE username=?");
$stmt->execute([$_POST['username']]);
$user = $stmt->fetch();
// verifying the password
if ($user && password_verify($_POST['password'], $user['password']))
{
// starts the session created if login info is correct
session_start();
$_SESSION['username'] = $user['username'];
header("Location: members.php");
exit;
} else {
$error = "Login and password don't match";
}
}
在这里,我们只需要检查一个用户是否存在 以及密码是否匹配就可以了。