Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Signin page to direct user back to their original page

    I am currently creating a website and am trying to make it so that once a user logs in they are taken back to the page they were on prior to signing in. I am unfortunately having difficulty, I can get the page that the user was on before fine but when I try to send the user back to that page after successful log in it doesn't work...please help.

    Any help is much appreciated.

    My php code for the signin page is as follows:



    PHP Code:
    <?php

    include("dbconnect.php");

    //posted back from the signin form
    $username $_POST['username'];
    $password $_POST['password'];
    $submit $_POST['signinsubmit'];

    //Gets the page that the user was on previously
    $page $_GET['page'];

    if(
    $submit != null)
    {
        if(
    $password == null)
            {
                
    $passwordcheck "failed";
            }
            
        if(
    $username == null)
            {
                
    $usernamecheck "failed";
            }
                    
        if(
    $passwordcheck == null && $usernamecheck == null)
            {
                
    $sqlquery"SELECT * FROM User WHERE Username = '$username' AND Password = md5('$password')";
                            
                
    $sqlresult mysql_query($sqlquery);
                
    $sqlcheck mysql_fetch_array($sqlresult);
                
    //If username and password are correct then do the following
                
    if($sqlcheck['Username'] <> NULL)
                {
                    
    //assign session
                    
    session_start();
                    
    $_SESSION['user'] = $username;
                
    //send user to previous page...not working 
                    
    header("location: " $page);
                    exit;
                }
                else
                {
                    
    $signinfailed yes;
                }
            }
    }
    ?>

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,464
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Your problem lies with this line:
    $page = $_GET['page'];

    You are getting to the sign-in page via a <form> with method="post",
    so you need to use $_POST['page'] ... but that's not the only thing
    to change ...

    On the page where they came from, you need to find out the name of the page
    (or script) they are currently on. This will give you the name, such as "index.php",
    or "contact.php" ... only the script name, not the directory or path.

    $script = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);

    Then ...

    Your form would look something like this ... with the hidden page value ...

    <?php $script = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1); ?>
    <form action="signin.php" method="post">
    <input type="hidden" name="page" value="<?=$script?>">
    Username: <input type="text" name="username"><br />
    Password: <input type="text" name="password"><br />
    <input type="submit" name="signinsubmit" value="Sign In">
    </form>
    Last edited by mlseim; 12-22-2010 at 02:05 AM.


  •  

    Tags for this Thread

    Posting Permissions

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