I have a structure like this:
myproject
layout
header.php
user
user.php
index.php
login.php
logout.php
header.php
is included in both index.php
and user.php
index.php
include code:
require_once(__DIR__ . 'layoutheader.php');
user.php
include code:
require_once(__DIR__ . '..layoutheader.php');
In header.php
I check if the user is logged in. If not logged in, The user is redirected to login.php
:
if(!logged_in()){
header('Location: ./login.php');
}
This works fine, If I visit index.php
. But not working with user.php
, As the URL becomes /user/login.php
which doesn’t exist.
And I can’t change header('Location: ./login.php');
to header('Location: ../login.php');
, Because it won’t work with index.php
.
So how to make this dynamic or make the header.php
directory the place where it goes back from?