Forum Moderators: coopster

Message Too Old, No Replies

php webpage access with $ SESSION

webpage access in php

         

ksugam

5:59 pm on Feb 6, 2008 (gmt 0)

10+ Year Member



Hello,
I have a site which needs user to login before accessing any webpage.
The way i do it currently is, i have the below mentioned code on each webpage:
session_start();
if (!isset($_SESSION['username'])) {
header("Location: index.php");
}

So if the user is not logged in, the user will be directed to the login page.

Now the problem is, after logging in, the user goes to the menu page and the menu has different links to different webpages....

How can i directly take the user where he intended to go directly without logging in?

e.g.:
the website flow is: login page -> Menu -> page1

Current implementation:
If the user directly enters page1 url without logging in, the user is sent to the login page. After logging in, the user goes to menu page.

Desired implementation:
After entering the url for page1, the user should be taken to login page and after successful login, the user should be taken back to page1 instead of menu.

Please help me out with this....

Thanks!

justgowithit

6:43 pm on Feb 6, 2008 (gmt 0)

10+ Year Member



First you need to identify visitors that are being redirected to the login page versus those that aren't. Then you need to figure out which page they were looking at first. You can use the same variable for both functions like:

if (!isset($_SESSION['username'])) {

if (isset($_SERVER['HTTP_REFERER'])) {
$ref = $_SERVER['HTTP_REFERER'];
} else {
$ref = '';
}

header("Location: index.php?r=".$ref);
}

--->

Then on the login page check if isset($_GET['r']) and that it's a valid URL using regex. If valid, store that value for use in the redirect after a successful login.