<?php
require_once "config.php";
require_once "session.php";
$error = '';
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit'])) {
$email = trim($_POST['email']);
$password = trim($_POST['password']);
// validate if email is empty
if (empty($email)) {
$error .= '<p class="error">Please enter an email.</p>';
}
// validate if password is empty
if (empty($password)) {
$error .= '<p class="error">Please enter your password.</p>';
}
if (empty($error)) {
if($query = $db->prepare("SELECT * FROM users WHERE email = ?")) {
$query->bind_param('s', $email);
$query->execute();
$row = $query->fetch();
if ($row) {
if (password_verify($password, isset($row['password']))) {
$_SESSION["userid"] = $row['id'];
// redirect the user to the welcome page
header("location: index.php");
exit;
} else {
$error .= '<p class="error">The password is not valid.</p>';
}
} else {
$error .= '<p class="error">No user exists by that email address.</p>';
}
}
$query->close();
}
// close connection
mysqli_close($db);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<form action="" method="post">
<h2>Login</h2>
<?php echo $error; ?>
<div class="form-group">
<label>Email Address</label>
<input type="email" name="email" class="form-control" required />
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="password" class="form-control" required />
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-primary" value="submit">
</div>
</form>
</div>
</div>
</body>
</html>
I've had quite some errors like if (password_verify($password, $row['password']))
said:
Notice: Trying to access array offset on value of type bool in C:\xampp\htdocs\defgonnawork\login.php on line 22
But I just threw a isset
there
database is properly connected, registration with the email and password was successful, I can see it saved in the db.
You are not getting the result. Try this
$result = $query->get_result();
/* now you can fetch the results into an array */
if($row = $result->fetch_assoc()){
//rest of the code
}