Jump to content

[SOLVED] Session Help


NoSalt

Recommended Posts

I am "attempting" to create a very simple admin login for a site I use but am having trouble with the sessions. It seems that I can log in correctly, the is a good session variable, and there is a session in my list of browser cookies. The only problem is that I cannot seem to stay logged in. Here is the trimmed down version of the page that I am using. Any idea what I am doing wrong here? Any help would be very much appreciated.

 

Thanks  :)

 

<?php
    $login = ($_GET['login']) ? $_GET['login'] : null;
    $submittedLogin = ($_POST['loginName']) ? $_POST['loginName'] : null;
    $submittedPW = ($_POST['password']) ? $_POST['password'] : null;

    require('./includes/config.inc.php');
    require('./includes/dbcx.inc.php');

    if($submittedLogin && $submittedPW){
        $sqlQuery = "select username,password from users where username='" . $submittedLogin . "'";
        $sql = mysql_query($sqlQuery);
        while($row = mysql_fetch_array($sql)){
            $loginName = $row['username'];
            $password = $row['password'];
        }

        if(md5($submittedPW) == $password && $submittedLogin == $loginName){
            if(session_start()){;
                $_SESSION['loggedIn'] = 1;
                $_SESSION['dummy'] = "Dummy Variable";
            }
        }
    }
?>
<html>
    <head>
        <title>
            Admin Page
        </title>
    </head>
    <body>
        <div class="container">
            <div>
                <h2>Admin Page</h2>
            </div>
<?php
    if(!isset($login)){
        if(isset($_SESSION['loggedIn'])){
?>
            <div>
                <table border="1">
                <tr><td colspan="2"><h4>Welcome Admin ...</h4></td></tr>
                <tr><th>The session var "loggedIn" is </th><td><?php echo $_SESSION['loggedIn']; ?></td></tr>
                <tr><th>The session var "dummy" is </th><td><?php echo $_SESSION['dummy']; ?></td></tr>
                <tr><th>The session id is </th><td><?php echo session_id(); ?><br /></td></tr>
                </table>
            </div>
<?php
        }
        else{
?>
            <div class="content">
                <table border="1">
                <tr><td colspan="2">Info Table</td></tr>
                <tr><th>The session var "loggedIn" is </th><td><?php echo $_SESSION['loggedIn']; ?></td></tr>
                <tr><th>The session var "dummy" is </th><td><?php echo $_SESSION['dummy']; ?></td></tr>
                <tr><th>The session id is </th><td><?php echo session_id(); ?><br /></td></tr>
                <tr><td colspan="2"><button onclick="window.location='<?php echo $_SERVER['PHP_SELF']; ?>?login=1';">Log In</button></td></tr>
                </table>
            </div>
<?php
        }
    }
    else{
?>
        <div class="loginDiv">
            <table border="1">
                <form name="theForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                <tr><th>Login Name:</th><td><input type="text" name="loginName" id="TextBox_01" /></td></tr>
                <tr><th>Password:</th><td><input type="password" name="password" id="TextBox_02" /></td></tr>
               </form>
            </table>
        </div>
<?php
    }
?>
        </div>
    </body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/71377-solved-session-help/
Share on other sites

Yes, it wants me to log in again. The very first page just has a "Log In" button. Click that and it takes you to a form that has a username and password field. Click the "submit" button and it takes you to the admin page with a table showing the sessionID and other information. If I go all the way back to the first page with the "Log In" button, the page doesn't recognize the session. However, when I "log in" again I get the same sessionID as before. Am I just completely overlooking something here?

 

Thanks for reading

Link to comment
https://forums.phpfreaks.com/topic/71377-solved-session-help/#findComment-359192
Share on other sites

Nope ... just calling session_start() once. Here is a copy of the code that is fully self-contained:

 

<?php
    $login = ($_GET['login']) ? $_GET['login'] : null;
    $submittedLogin = ($_POST['loginName']) ? $_POST['loginName'] : null;
    $submittedPW = ($_POST['password']) ? $_POST['password'] : null;

    $loginName = "admin";
    $password = "5f4dcc3b5aa765d61d8327deb882cf99"; // the md5 hash for the word "password"

    if(md5($submittedPW) == $password && $submittedLogin == $loginName){
        if(session_start()){
            $_SESSION['loggedIn'] = 1;
            $_SESSION['dummy'] = "Dummy Variable";
        }
    }
?>
<html>
    <head>
        <title>
            Admin Page
        </title>
    </head>
    <body>
        <div class="container">
            <div>
                <h2>Admin Page</h2>
            </div>
<?php
    if(!isset($login)){
        if(isset($_SESSION['loggedIn'])){
?>
            <div>
                <table border="1">
                <tr><td colspan="2"><h4>Welcome Admin ...</h4></td></tr>
                <tr><th>The session var "loggedIn" is </th><td><?php echo $_SESSION['loggedIn']; ?></td></tr>
                <tr><th>The session var "dummy" is </th><td><?php echo $_SESSION['dummy']; ?></td></tr>
                <tr><th>The session id is </th><td><?php echo session_id(); ?><br /></td></tr>
                 </table>
            </div>
<?php
        }
        else{
?>
            <div class="content">
                <table border="1">
                <tr><td colspan="2">Info Table</td></tr>
                <tr><th>The session var "loggedIn" is </th><td><?php echo $_SESSION['loggedIn']; ?></td></tr>
                <tr><th>The session var "dummy" is </th><td><?php echo $_SESSION['dummy']; ?></td></tr>
                <tr><th>The session id is </th><td><?php echo session_id(); ?><br /></td></tr>
                <tr><td colspan="2"><button onclick="window.location='<?php echo $_SERVER['PHP_SELF']; ?>?login=1';">Log In</button></td></tr>
                </table>
            </div>
<?php
        }
    }
    else{
?>
        <div class="loginDiv">
            <table border="1">
                <form name="theForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                <tr><th>Login Name:</th><td><input type="text" name="loginName" id="TextBox_01" /></td></tr>
                <tr><th>Password:</th><td><input type="password" name="password" id="TextBox_02" /></td></tr>
                <tr><td><button type="submit">Log-In</button><button type="reset">Reset</button></td></tr>
                </form>
            </table>
        </div>
<?php
    }
?>
        </div>
    </body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/71377-solved-session-help/#findComment-359210
Share on other sites

Well ... BlueSkyIS nailed it. I simply moved the session_start() to the top of the page and everything worked like a champ. Sometimes I need to be actually hit over the head with the <insert name of language> book before a simple concept sinks in. Thank you all for looking and replying. Have a great day!

 

:)

Link to comment
https://forums.phpfreaks.com/topic/71377-solved-session-help/#findComment-359215
Share on other sites

shocker-z was close first, except you CAN leave a page WITH session_start() and go to multiple pages WITHOUT session_start() and then return to a page WITH session_start() and continue the original session, something like this:

 

login.php // session_start(), set session variables

somepage1.php // session_start(), session variables still set from login.php

somepage2.php // NO session_start(), session variables are not available in code

somepage3.php // NO session_start(), session variables are not available in code

somepage4.php // session_start(), session variables still set from login.php (except in case of session timeout, of course)

Link to comment
https://forums.phpfreaks.com/topic/71377-solved-session-help/#findComment-359219
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.