0

Hello guys i have a problem with php sessions. I try to send a variable from my second page to my third page but i keep getting undefined variable i will include code from all three pages i am sorry in advance for the pretty large amount of code but i belive it will be easier for you guys to help then. Thanks in advance

PAGE 1

<form method="POST" action="authorize.php">
            <input type="text" name="username" class="username"><br>
            <input type="password" name="password" class="password"><br>
            <label for ="remeberMe">Remember me!</label>
            <input type="checkbox" name="remeberMe" class="rememberMe"><br>
            <input type="submit" name="submitLogin" class="submitLogin" value="Login!">
        </form>

PAGE 2

    session_start();
    include 'connection.php';

    $user = $_POST['username'];
    $pass = $_POST['password'];

    $sql = mysqli_query($con, "SELECT * FROM users WHERE screename = '$user' AND ...");
    $count = mysqli_num_rows($sql);

    if ($count == 1)
    {
        $login = true;
    }
    else
    {
        $login = false;
    }   

    if ($login == true)
    {
        $_SESSION['access'] = true;
        $_SESSION['username'] = $_POST['username']; 
        header("Location:"."mainpage.php?".SID);
        exit;
    }   
    else 
    {
        header("Location:"."index.php?");
        exit;
    }   

PAGE 3

    session_start();

    if($_SESSION['access'] != true)
    {
        include("index.php");
        exit;
    }

    if(isset($_POST['username']))
    {
        $user = $_POST['username'];
    }

ERROR: $user on page 3 is undefined

17
  • 3
    Sidenote: This line is unfinished $sql = mysqli_query($con,... - bad paste? Commented Nov 11, 2014 at 17:06
  • Which file is authorize.php? And which values/ variables you want to pass torught which files? Commented Nov 11, 2014 at 17:06
  • $sql = mysqli_query($con, "SELECT * FROM users WHERE screename = '$user' AND .... is that your code? Commented Nov 11, 2014 at 17:06
  • 1
    edit it........ also mention, what undefined variable Commented Nov 11, 2014 at 17:07
  • 1
    @AeroX I saw that. Sometimes when there's info missing from a query, could have some bearing on the outcome. For instance, if the missing query code is like WHERE screename = '$user' AND column = '".$_SESSION['var']."' (wink). I just like to see all the parts. Commented Nov 11, 2014 at 17:18

3 Answers 3

2

You're accessing a POST variable rather than the one you saved in your session. Change to:

if(isset($_SESSION['username']))
{
    $user = $_SESSION['username'];
}
Sign up to request clarification or add additional context in comments.

Comments

1

So first you have to limit your SQL Statement with LIMIT 1 or change this condition to >=:

if ($count == 1) //'>='
{
    $login = true;
}
else
{
    $login = false;
}   

After that you have to change on page 3 this to the $_SESSION array:

if(isset($_POST['username'])) //to `$_SESSION['username']`
{
    $user = $_POST['username']; //to `$_SESSION['username']`
}

2 Comments

Worked perfect thanks! and thanks to everyone else that contributed! if you dont mind me asking why did i made a difference that i changed it to >=1 ? :)
If your not limiting your query the count could be 2 or 3 so the condition to == 1 wouldn't be true! So if you change it to >= your sure that the query has at min. 1 row or more!
1

The undefined variable, are you referring to the $_POST['username'] or the session variable in page 3? The post variable would not be retained in page 3, you need to refer to the session variable $_SESSION['username'].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.