My login script is displaying the following error message:
Warning: Illegal string offset 'hash' on line 30.
Here is the code that generates the error:
<?php
// configuration
require("../../includes/config.php");
// if form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
// validate submission
if (empty($_POST["username"]))
{
adminapologize("You must provide your username.");
}
else if (empty($_POST["password"]))
{
adminapologize("You must provide your password.");
}
$username = $_POST["username"];
// query database for user
$rows = "SELECT * FROM admin WHERE username = '$username'";
// if we found user, check password
if (count($rows) == 1)
{
// first (and only) row
$row = $rows[0];
// compare hash of user's input against hash that's in database
if (crypt($_POST["password"], $row["hash"]) == $row["hash"])
{
// remember that user's now logged in by storing user's ID in session
$_SESSION["admin_id"] = $row["admin_id"];
// redirect to admin home
redirect("index.php");
}
}
// else apologize
adminapologize("Invalid username and/or password.");
}
else
{
// else render form
adminrender("login_form.php", ["title" => "Admin Log In"]);
}
?>
The error seems to be coming from this line:
if (crypt($_POST["password"], $row["hash"]) == $row["hash"])