Hi guys
I want to redirect my members to the page they were on before they logged in or were trying to access
My problem is they are redirected to the login page, when they access a members page and dont know what code to use
How can I do this?
| SitePoint Sponsor |




Hi guys
I want to redirect my members to the page they were on before they logged in or were trying to access
My problem is they are redirected to the login page, when they access a members page and dont know what code to use
How can I do this?
Before redirecting user to the login page, just save previous location in $_SESSION.
e.g.
in restricted.php:
somewhere in login.php:PHP Code:$_SESSION['last_page'] = 'restricted.php';
header("Location: login.php");
exit;
PHP Code:if (isset($_SESSION['last_page'])) {
header("Location: " . $_SESSION['last_page']);
exit;
} else {
header("Location: index.php");
exit;
}


Do you mean if they were browsing a non-specific page and then had to login, return them to THAT page.....
If so you will need to look at $_SERVER['HTTP_REFERER'] which will give you the page they came from. It's not totally foolproof as some hosts dont enable it but it is generally available.
Other than that you could use either of the solutions above and preators looks like the better option. (no offense iktorn and welcome to the forums)
Mike Swiffin - Community Team Leader
Only a woman can read between the lines of a one word answer.....
I started out with nothing... and still got most of it left!




the problem is they don't click on a log link
when they access as page where they need to be a member it redirects them straight to login
Then use cookies. Or when you do the redirect, add the 'return=' parameter .
Code PHP:header('Location: login.php?return='.SITE_URL.$_SERVER['PHP_SELF']);




please read post below




what about if there is a field at the end of the url
like
http://www.notexperienced.co.uk/apply.php?id=22
its only returning
http://www.notexperienced.co.uk/apply.php
any help I would be thankful





there are many ways to go about this, but in essence, you need to follow these steps.
1. determine if the user is logged in
2. if not logged in, create a session variable (or append the current page as a $_GET variable as suggested
3. use header() to go to the login page
4. once username and password are accepted, check for a redirect with any of the methods above, and use header again to redirect the user to that page.
EDIT: I noticed you may want to include $_GET variables in the redirect. This is possible, you'll just have to take a few extra steps formatting the redirect url. eg.
PHP Code:if (isset($_GET['id'])) $redirect .= '?id='.$_GET['id']; // yes this is dirty code, clean before using
Studiotime - Time Management for Web Developers
to-do's, messages, invoicing, reporting - 30 day free trial!
Thomas Multimedia Web Development
Just use REQUEST_URI instead
I haven't tested though. It may need urlencode at some point.Code PHP:header('Location: login.php?return='.SITE_URL.$_SERVER['REQUEST_URI']);





I have a feeling that REQUEST_URI is not entirely reliable, prone to the same problems as HTTP_REFFERER.
I think PHP_SELF and all the others are a safer bet. actually, isn't there a QUERY_STRING to grab all the $_GET variables rather than testing for them before header()?
Studiotime - Time Management for Web Developers
to-do's, messages, invoicing, reporting - 30 day free trial!
Thomas Multimedia Web Development
REQUES_URI is just the URI of your request. The only problem that I know of is only if you have some rewrite rules in apache that change your URL.





it seems your right, REQUEST_URI will give you the path and the get variables so it seems like a perfect match.
Studiotime - Time Management for Web Developers
to-do's, messages, invoicing, reporting - 30 day free trial!
Thomas Multimedia Web Development
Bookmarks