The code is suppose to set a variable if the password and username are inserted. But $_SESSION variable is never set and I don't know why and it just keeps redirecting me to the login page.
loginform.php:
<?php
$server = 'hiddenforthispost';
$username = 'hiddenforthispost';
$password = 'hiddenforthispost';
$schema = 'hiddenforthispost';
$pdo = new PDO('mysql:dbname=' . $schema . ';host=' . $server, $username, $password,
[ PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION]);
if (isset($_POST['Submit'])) {
if(empty($_POST['Username']) || empty($_POST['Password'])) {
echo '<p> You must insert all of the fields! </p>';
}
else {
$stmt = $pdo->prepare('SELECT * FROM logins
WHERE login_name = :username');
$criteria = [
'username' => $_POST['Username']
];
$stmt->execute($criteria);
$user = $stmt->fetchColumn(1);
echo $user;
if (password_verify($_POST['Password'], $user)) {
session_start();
$_SESSION['loggedin'];
header('location: adminpage.php');
}
else {
echo '<p> Wrong password </p>';
}
}
}
?>
The page where I want only logged in users to access: adminpage.php:
<?php
session_start();
if(!isset($_SESSION['loggedin'])){
header('location: loginpage.php');
}
else {
}
?>