I'm making a custom Content Management System for a website I built, and so far everything is looking good up until this road block. I got my code to work to where from login.php page you can enter in the username and password and it successfully redirects me to the index.php page but then I decided that I need to redirect anyone who isn't logged in to login.php before looking at other pages on my CMS.
So in comes this code right here...
<?php require_once("lib/sessions.php");
require_once("lib/functions.php");
require_once("meta.php");
login_confirm();
?>
That is at the top of each page in my CMS and that login_confirm function looks like this...
function login_confirm() {
if(!logged_in()) {
redirect_to("login.php");
}
}
function logged_in() {
return isset($_SESSION['admin_id']);
}
The $_SESSION['admin_id'] gets set on my login page like this...
$found_admin = attempt_login($username, $password);
if($found_admin) {
//Success, mark user as logged in
$_SESSION["admin_id"] = $found_admin["id"];
$_SESSION["username"] = $found_admin["username"];
redirect_to("index.php");
} else {
//failed
$_SESSION["message"] = "Log in failed";
}
And like I said in the beginning, the login page will successfully redirect you to index.php no problem, but that was before I put login_confirm() at the top of my pages. Now no matter what I always get redirected to login.php even though I typed in the right credentials. I assume the Session isn't saving or something...
Edits: session.php looks like this. So the session is being started at the top of the page.
<?php
session_start();
ob_start();
//function for message and errors
?>