SitePoint Sponsor |
|
User Tag List
Results 1 to 13 of 13
-
Jun 14, 2007, 14:36 #1
- Join Date
- Oct 2004
- Location
- uk
- Posts
- 853
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Redirect back to page before login
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?
-
Jun 14, 2007, 14:54 #2
- Join Date
- Sep 2005
- Location
- Poland
- Posts
- 23
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Before redirecting user to the login page, just save previous location in $_SESSION.
e.g.
in restricted.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;
}
-
Jun 14, 2007, 22:40 #3
-
Jun 15, 2007, 00:55 #4
- Join Date
- Aug 2004
- Location
- Manchester UK
- Posts
- 13,807
- Mentioned
- 158 Post(s)
- Tagged
- 3 Thread(s)
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 Advisor
Only a woman can read between the lines of a one word answer.....
-
Jun 15, 2007, 02:06 #5
- Join Date
- Oct 2004
- Location
- uk
- Posts
- 853
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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
-
Jun 15, 2007, 02:30 #6
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']);
-
Jun 15, 2007, 03:47 #7
- Join Date
- Oct 2004
- Location
- uk
- Posts
- 853
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
please read post below
-
Jun 15, 2007, 04:14 #8
- Join Date
- Oct 2004
- Location
- uk
- Posts
- 853
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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
-
Jun 15, 2007, 05:56 #9
- Join Date
- Mar 2006
- Location
- Gold Coast, Australia
- Posts
- 1,369
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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
-
Jun 15, 2007, 05:57 #10
Just use REQUEST_URI instead
Code PHP:header('Location: login.php?return='.SITE_URL.$_SERVER['REQUEST_URI']);
-
Jun 15, 2007, 06:21 #11
- Join Date
- Mar 2006
- Location
- Gold Coast, Australia
- Posts
- 1,369
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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
-
Jun 15, 2007, 07:12 #12
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.
-
Jun 15, 2007, 20:07 #13
- Join Date
- Mar 2006
- Location
- Gold Coast, Australia
- Posts
- 1,369
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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