I'm relatively new to PHP, but I know quite a bit. I am making a login form and I am checking whether they are logged in before they can enter pages and if they are not, then they are redirected to the login page, I am using sessions. Every time that a user logs in or registers it creates a session using:
$_SESSION['username'] = '$username';
On every page that the user is not allowed to access, I include, checklogin.php - a file I made, with this code:
<?php
function checklogin() {
session_start();
if(!isset($_SESSION['myusername'])){
return false;
header('location: login.php');
} else {
return true;
}
}
checklogin(); //checks login
?>
However it doesn't seem to be working as you can access pages when not logged in. Also, there is an if statement:
if(!isset($_SESSION['myusername'])){
This is used to determine what is shown in the header.
None of this is working, so what am I doing wrong? Any help would be greatly appreciated.