Link to home
Start Free TrialLog in
Avatar of Marius0188
Marius0188

asked on

PHP - Login Include redirect to initial page

Dear Experts,

I've implemented a login include at the top of each of my pages.
When a user tries to open this pages it will first check whether he has been logged in, using $_SESSION's,
and if not, the user is directed to the login page.

This is working as expected up till here. But what I would like to have is when the user tries to open a page the login include must direct the user to the page he was initially trying to open after the login was successful.

Hope this makes sense..

Kind regards
SOLUTION
Avatar of isaackhazi
isaackhazi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of gamebits
gamebits
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Improved code regarding the answer above (this is, passing the referrer URL as a GET variable)
 

// Code for redirecting an non logged in user to login page
 
$loginurl = 'login.php';
header('Location: '. $loginurl . "?from=" . urlencode($_SERVER['PHP_SELF']));
 
 
// Code for redirecting a logged in user to referrer page
 
header('Location: ' . urldecode($_GET['from']));

Open in new window