SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Guru
    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?

  2. #2
    SitePoint Member
    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; 
    somewhere in login.php:

    PHP Code:
    if (isset($_SESSION['last_page'])) {
      
    header("Location: " $_SESSION['last_page']);
      exit;
    } else {
      
    header("Location: index.php");
      exit;


  3. #3
    SitePoint Evangelist praetor's Avatar
    Join Date
    Aug 2005
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The above solution needs that you have a session already started. A more generic method is to have the return page coded in url.
    Code PHP:
    define ('SITE_URL','http://www.yourdomain.com');
     
    echo '<a href="login.php?return='.urlencode(SITE_URL.$_SERVER['PHP_SELF']).'">Login</a>';

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    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.....

  5. #5
    SitePoint Guru
    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

  6. #6
    SitePoint Evangelist praetor's Avatar
    Join Date
    Aug 2005
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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']);

  7. #7
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    please read post below

  8. #8
    SitePoint Guru
    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

  9. #9
    SitePoint Wizard wheeler's Avatar
    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

  10. #10
    SitePoint Evangelist praetor's Avatar
    Join Date
    Aug 2005
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just use REQUEST_URI instead
    Code PHP:
    header('Location: login.php?return='.SITE_URL.$_SERVER['REQUEST_URI']);
    I haven't tested though. It may need urlencode at some point.

  11. #11
    SitePoint Wizard wheeler's Avatar
    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

  12. #12
    SitePoint Evangelist praetor's Avatar
    Join Date
    Aug 2005
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  13. #13
    SitePoint Wizard wheeler's Avatar
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •