Redirect user back to original page after login

Discussion in 'PHP & Perl' started by sfidirectory, Aug 25, 2011.

  1. sfidirectory

    sfidirectory Senior Member

    Mar 29, 2010
    Likes Received:
    Web developer/BTC enthusiast
    php artisan make:migration
    Home Page:
    Hi everyone,

    So I have finished Stage 1 of a project of mine (mentioned on Not sure how to ask what I want to ask so I will describe it in a scenario:

    So I am wondering what can I implement in my login processing script to enable that? At the moment I am using sessions and php. I can say in the login processing script, if the username and password match to the corresponding rows in the database, the header is changed and the user is directed to their account page. I am not sure how to do something similar to this except the user gets directed to the restricted page/original page they wanted to see in the first place...

    Thanks in advance!
  2. Autumn

    Autumn Elite Member

    Nov 18, 2010
    Likes Received:
    I figure out ways to make money online and then au
    You serve the restricted page, and since they're not authenticated, you show the login form and a message that says "you must login to view this page blah blah." In the login form, embed the current url of the restricted page as a hidden field eg. restricted_url="" If the user logs in through the default login page, don't add that hidden field to the login form.

    After a successful login, check for the presence of $_POST['restricted_url']. If it's there, issue the "thianks for logging in" message and then issue a meta refresh to $_POST['restricted_url'], similar to what happens when you log in to BHW. If $_POST['restricted_url'] isn't present in the $_POST array, then just send the surfer to their account page. You could just issue a 301 header instead of a meta refresh of course.
    • Thanks Thanks x 1
  3. xenon2010

    xenon2010 Regular Member

    Apr 27, 2010
    Likes Received:
    web and desktop apps programmer
    well you can achieve that easily using a $_GET variable.
    lets say the page the user wants to visit was:

    now when you visit that page without logging in. it will automatically redirect you back to login page with this $_GET variable:
    now once you enter correct login details your script needs to read $_GET['redirect'] variable then put it inside redirection function to transfer you to the previous page..
    thats simply it