我完全承认一定对关联数组的构造有错误的理解。
以下登录脚本将使用关联数组填充 $userdata,该数组由 $username 的散列密码和从 SQL Server 数据库(具体为 Azure SQL)查询的盐组成。但是,用于创建所提供密码的哈希并与在数据库中找到的哈希密码进行比较的代码部分失败,错误表明 $userdata[password] 和 $userdata[salt] 未定义。
<?php
$username = $_POST['username'];
$password = $_POST['password'];
// Connect to SQL Server
include '../../phpconfig/connectstrings.php';
try
{
$conn = new PDO ( "sqlsrv:server = $serverstringname; Database = $databasestringname", "$usernamestringname", "$passwordstringname");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION, );
}
catch ( PDOException $e )
{
print( "Error connecting to SQL Server." );
die(print_r($e));
}
catch(Exception $e)
{
die(var_dump($e));
}
//Query database for the hashed password and salt for the supplied username
if(!empty($_POST)) {
try
{
$sql_select = $conn->prepare("SELECT password, salt FROM logins WHERE username = '$username'");
$sql_select->execute();
}
catch(Exception $e)
{
die(var_dump($e));
}
//Fetch all of the remaining rows in the result set
$userdata = $sql_select->fetchAll(PDO::FETCH_ASSOC);
//check for a valid username
if(empty($userdata))
{
echo "User: $username was not found";
die;
}
//hash the queried salt and hash the supplied password
$hash = hash('sha256', $userdata['salt'] . hash('sha256', $password) );
//compare the hashed salted password supplied with that queried from database
if($hash = $userdata['password'])
{
echo "Welcome, $username!";
}
else
{
echo "Invalid password";
}
}
?>
虽然我不怀疑从 $sql_select 获取数组之外的一些代码需要一些调试,但我无法做到这一点,因为 $userdata 似乎将所有关联数组数据分配给变量的单个部分,如所示以下转储的输出:
var_dump($sql_select);
//output = object(PDOStatement)#2 (1) { ["queryString"]=> string(61) "SELECT password, salt FROM logins WHERE username = 'mrtactics'" }
list($a[0], $b[1]) = $userdata;
var_dump($a);
var_dump($b);
//output = array(1) { [0]=> array(2) { ["password"]=> string(64) "f24704c0ce72a618cf1738894ebdd6001f4d3329802ab83bd418df66cbc46b1a" ["salt"]=> string(3) "6e0" } } array(1) { [1]=> NULL }
var_dump($userdata["salt"]);
//output = NULL
var_dump($userdata['salt']);
//output = NULL
var_dump($userdata['password']);
//output = NULL
foreach ($userdata as $item => $value)
echo "$item: $value<br>";
//output = 0: Array
$password = $sql_select->fetchColumn(0);
$salt = $sql_select->fetchColumn(1);
var_dump($password);
var_dump($salt);
//output = string(64) "f24704c0ce72a618cf1738894ebdd6001f4d3329802ab83bd418df66cbc46b1a" bool(false)
显而易见的解决方法是查询提供的用户名的单个值并传递每个继承人各自的变量。但是,这需要对数据库进行两次必要的调用,而且我对如何构造关联数组以及如何使用存储在其中的信息一无所知。
我怀疑我要么为我试图从中检索的方法获取错误构造的对象,要么我的语法很糟糕。我确实打算继续使用 PDO 而不是 sql_* 命令。
编辑:让我们让它更简单,然后:
$userdatasql = $sql_select->fetchAll(PDO::FETCH_ASSOC);
$userdata['password']="f24704c0ce72a618cf1738894ebdd6001f4d3329802ab83bd418df66cbc46b1a";
$userdata['salt']="6e0";
var_dump($userdata);
var_dump($userdatasql);
var_dump($userdata['password']);
var_dump($userdatasql['password']);
//Dump of $userdata = array(2) { ["password"]=> string(64) "f24704c0ce72a618cf1738894ebdd6001f4d3329802ab83bd418df66cbc46b1a" ["salt"]=> string(3) "6e0" }
//Dump of $userdatasql = array(1) { [0]=> array(2) { ["password"]=> string(64) "f24704c0ce72a618cf1738894ebdd6001f4d3329802ab83bd418df66cbc46b1a" ["salt"]=> string(3) "6e0" } }
注意到这 2 个数组的构造不同了吗?我不知道这到底是什么意思,这就是我在这里的原因。如果我猜测 $userdatasql 数组似乎在数组中包含一个数组,因此必须对调用进行索引。
//Dump of $userdata['password'] = string(64) "f24704c0ce72a618cf1738894ebdd6001f4d3329802ab83bd418df66cbc46b1a"
//Dump of $userdatasql['password'] = NULL
更多信息:
echo (count($userdata));
echo (count($userdatasql));
//output = 2
//output = 1
echo (count($userdata, 1));
echo (count($userdatasql, 1));
//output = 2
//output = 3
这告诉我,由 PDO FETCH_ASSOC 创建的数组与手动创建的数组具有不同的结构,但包含相同的 2 条数据和相同的 2 个索引。
有了这些知识,我修改了我的转储以包含 0 索引位置,然后突然输出了预期的数据:
var_dump($userdatasql['0']['password']);
var_dump($userdatasql['0']['salt']);
//password dump = string(64) "f24704c0ce72a618cf1738894ebdd6001f4d3329802ab83bd418df66cbc46b1a"
//salt dump = string(3) "6e0"
这是否意味着我必须按索引引用所有 PDO FETCH ASSOC 数组?
我不应该这么想,因为我发现没有代码示例表明这一点。
那么,为什么我的 PDO FETCH ASSOC 阵列格式不正确?