您好,我无法理解为什么我的身份验证不断失败。我尝试了许多调试方法,我想我很难过。请记住,我仍然是初学者,目前安全性不是问题。我只是想让它工作。
所以对于我的注册脚本,我让用户输入用户名和密码
$username = $_POST['username'];
$password = $_POST['password'];
// Post 正在保存的数据
$createTable = $db->prepare("CREATE TABLE `$username` (Title varchar(40) ,
About varchar(1000) ,
Chapter int(4),
Img varchar(100) ,
Url varchar(50)) ");
$createTable->execute();
/// 为用户创建一个表
$Register = $db->prepare("INSERT INTO library (id,
username, password) VALUES ($id ,
'$username','$password')");
$Register ->execute();
// 将数据插入到库表中。图书馆有用户和密码
echo 'user sucessfully registered';
$_SESSION['user'] = $username;
$_SESSION['pass'] = $password;
我遇到的问题是对此进行身份验证以进行登录。使用两个 SESSION 变量作为参数调用登录函数。
function login($SentUser , $SentPass)
{
echo 'trying to log in '.'<br>';
require_once 'Connection.php';
$user = $SentUser;
$pass = $SentPass;
/// check data base for match user and pass
$sth = $db->prepare('SELECT username , password from `library` where username = ? And password = ? ');
$sth->bindParam(1, $user);
$sth->bindParam(2, $pass);
$sth->execute();
while ($row = $sth->fetch(PDO::FETCH_ASSOC))
{
echo 'user : ' . $row['username'] , '<br>';
echo 'pass: ' . $row['password'] , '<br>';
echo '<br>';
}
.
// fetchcolumn returns true false if select took a row or not.
if($sth->fetchColumn() != false) { /// This is where the code fails as it almost always goes to the else case.
$_SESSION['user'] = $user;
$_SESSION['pass'] = $pass;
$_SESSION['passed'] = TRUE;
echo 'Welcome '.$user.'<br>';}
else
{
echo 'failed authenticate'.'<br>';
}
为了更加清楚,这里是被调用的connection.php。它基本上只是将我连接到数据库,为我提供 $db 对象。
<?php
$config['db'] = array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'mywebsite'
);
$db = new PDO('mysql:host=' . $config['db']['host']. ';dbname=' . $config['db']['dbname'] , $config['db']['username'] , $config['db']['password']);
?>