I have a couple of pages protected with a login; add.php, settings.php and archive.php
At the top of each of these pages i have this code:
// check login
session_start();
if(!isset($_SESSION['blog_login'])){
$_SESSION['last_visited'] = $_SERVER['REQUEST_URI'];
header("Location: login.php");
exit();
}
When logging out, the page request goes to logout.php. Logout.php looks like below:
session_start();
unset($_SESSION['blog_login']);
header("Location: login.php");
When logging in again, i want to go to the page i was before. This is my login.php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$error = NULL;
if(isset($_POST['username'],$_POST['password'])){
$user = array(
"username" => $admin_name,
"password"=> $admin_passw
);
$username = $_POST['username'];
$pass = $_POST['password'];
if($username == $user['username'] && $pass == $user['password']){
session_start();
$_SESSION['blog_login'] = $username;
header('Location:'.$_SESSION['last_visited']);
}
else {
$error = '<div class="alert alert-danger">Incorrect login data</div>';
}
}
}
Unfortunately, the header('Location:'.$_SESSION['last_visited']); line does not send me to the previous page i was before logging out.
What i am doing wrong here?