1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Redirect user back to original page after login

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

  1. sfidirectory

    sfidirectory Senior Member

    Joined:
    Mar 29, 2010
    Messages:
    899
    Likes Received:
    483
    Occupation:
    Web developer/BTC enthusiast
    Location:
    php artisan make:migration
    Home Page:
    Hi everyone,

    So I have finished Stage 1 of a project of mine (mentioned on http://www.blackhatworld.com/blackhat-seo/php-perl/344754-how-do-you-temporarily-ban-user.html)... 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

    Joined:
    Nov 18, 2010
    Messages:
    2,197
    Likes Received:
    3,041
    Occupation:
    I figure out ways to make money online and then au
    Location:
    Spamville
    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="http://yoursite.com/restricted_page.php." 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

    Joined:
    Apr 27, 2010
    Messages:
    231
    Likes Received:
    48
    Occupation:
    web and desktop apps programmer
    Location:
    prison
    Home Page:
    well you can achieve that easily using a $_GET variable.
    lets say the page the user wants to visit was: http://www.somthing.com/member.php?id=1

    now when you visit that page without logging in. it will automatically redirect you back to login page with this $_GET variable: http://www.somthing.com/login.php?redirect=member.php?id=1
    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