Beginner here. I changed my login form from
$q = "SELECT id, fname, lname FROM users WHERE (username='$username' AND password=SHA1('$password'))";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br>MySQL Error: " . mysqli_error($dbc));
if (@mysqli_num_rows($r) == 1) {
$_SESSION = mysqli_fetch_array ($r, MYSQLI_ASSOC);
mysqli_free_result($r);
mysqli_close($dbc);
$url = BASE_URL . (isset($_SESSION['redirect']) ? $_SESSION['redirect'] : 'index.php');
unset($_SESSION['redirect']);
ob_end_clean();
header("Location: $url");
exit();
} else {
echo 'Message';
}
to this
$q = "SELECT id, fname, lname FROM users WHERE (username='$username' AND password=SHA1('$password'))";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br>MySQL Error: " . mysqli_error($dbc));
if (@mysqli_num_rows($r) == 1) {
$row = mysql_fetch_assoc($r);
$_SESSION['id'] = $row['id'];
$_SESSION['fname'] = $row['fname'];
$_SESSION['lname'] = $row['lname'];
mysqli_free_result($r);
mysqli_close($dbc);
$url = BASE_URL . (isset($_SESSION['redirect']) ? $_SESSION['redirect'] : 'index.php');
unset($_SESSION['redirect']);
ob_end_clean();
header("Location: $url");
exit();
} else {
echo 'Message<p>';
}
And the login form is no longer working. So, I basically changed my form from setting all rows as session variables to individually setting rows as session variables. I needed to do this so the redirect would work. Not sure if I've incorrectly set the session variables or what the problem is but I can no longer log in using the form (on submit the page just refreshes without logging the user in).
EDIT: Solution to this problem is posted below. It was to replace
$row = mysql_fetch_assoc($r);
with
$row = mysqli_fetch_array ($r, MYSQLI_ASSOC);
According to this post, mysql_fetch_array( $result, MYSQL_ASSOC ) = mysql_fetch_assoc( $result ). However, login didn't work when the switch was made when it should have.