Results 1 to 4 of 4

Thread: Login form that redirects to the page it came from

  1. #1
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default Login form that redirects to the page it came from

    Hi,

    This login form is in my website's template header file and php included so it shows on all pages of my website.

    Code:
    <form action="login.php" method="post" enctype="multipart/form-data">
    Username: <input type="text" name="username"><br>
    Password: <input type="password" name="password"><br>
    Remember Me? <input type="checkbox" name="autologin"><br>
    <input type="hidden" name="redirect" value="index.php"> 
    <input type="submit" value="Login" name="login">
    </form>
    At the moment, a user can log in from any page but the hidden redirect always sends the user back to the index page.

    I would like the form to send the user back to the page where they logged in from so its less confusing.

    Is there a script that can do this or do I have to resort to putting a login form in every page and customising the redirect url?

    Any advice is welcome.

    Thanks

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Try this. (not tested.) Make sure all your pages are named whatever.php instead of .html.

    PHP Code:
    <form action="login.php" method="post" enctype="multipart/form-data">
    Username: <input type="text" name="username"><br>
    Password: <input type="password" name="password"><br>
    Remember Me? <input type="checkbox" name="autologin"><br>
    <input type="hidden" name="redirect" value="<?php echo $_SERVER['PHP_SELF']; ?>"> 
    <input type="submit" value="Login" name="login">
    </form>

  3. #3
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Hi traq,

    Thanks for replying. I tried your suggestion along with $_SERVER['HTTP_REFERER']); but they didnt work for me.

    It got my cogs turning though cos I found a workaround - I created a new page called "redirect.php" with this code in it:
    Code:
    <?php
    header('Location:' . $_SERVER['HTTP_REFERER']);
    exit();
    ?>
    I then changed the redirect url to point to it like: <input type="hidden" name="redirect" value="redirect.php"> and it does the job brilliantly.

    Thanks

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    glad you got it working. I figured that the hidden input was used by some part of the login page, so without seeing that page, it was kinda a shot in the dark as to whether it would work or not.

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
  •