简体   繁体   English

登录后不保存会话数据

[英]Not saving session data after log in

Hey there im trying to make a log in and register system where when the user log in in the header it shows him a message greeting him with his username and when he is not logged it shows him a message greeting him as a guest. 嘿那里我试图登录并注册系统,当用户登录标题时,它向他显示一条消息,用他的用户名问候他,当他没有登录时,它会向他显示一条消息,称他为客人。 My problem is i suppose that in the header page (member.php page) the sessions are not saved and the user is never logged in. While testing my code . 我的问题是,我认为在页眉页面(member.php页面)中,会话未保存,用户从未登录。在测试我的代码时。 The user after logging in successfully it redirect him to member.php page where it should show him the username and all that. 用户登录成功后,将其重定向到member.php页面,在那里它应该显示用户名和所有内容。 Yet it doesn't. 但事实并非如此。 Here are my codes : 这是我的代码:

member.php : member.php:

<?php
error_reporting(E_ALL ^ E_NOTICE);
error_reporting(0);
session_start();
  $_SESSION['userid'] = $dbid;
  $_SESSION['username'] = $dbuser;
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
      <head>

      <LINK REL=StyleSheet HREF="css/styletest.css" TYPE="text/css">

      </head>
<body>

<div id="wrapper">

<!-- Website menu -->

        <div id="menu-bar">
            <li><a href="">Testing links</a></li>
            <li><a href="">Testing links</a></li>
            <li><a href="">Testing links</a></li>
            <li><a href="">Testing links</a></li>
            <li><a href="">Testing links</a></li>
            <li><a href="">Testing links</a></li>
        </div>  

<!-- End of Website menu -->

<div id="content">

<!-- Website header -->

        <div id="header">
            <h1>Testing header</h1>
      <?php

       if ($username && $userid) {

              echo "Welcome <b>$username</b>, <a href='./logout.php'>Logout</a>";

       } else {

              echo "Please login to access this page. <a href='./login.php'>Login here</a>";

       }

        ?>
        </div>  

<!-- End of Website header -->  

<!-- -----Website content----- -->      

<p>Testing website content</p>

<!-- End of Website content -->     
</div>

<!-- Website footer --> 

        <div id="footer">

        </div>

<!-- End of Website footer -->      

</div>

</body>

</html>

And here is login.php : 这是login.php:

<?php
error_reporting(E_ALL ^ E_NOTICE);
error_reporting(0);
session_start();
$_SESSION['userid'] = $dbid;
$_SESSION['username'] = $dbuser;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
      <head>

      <LINK REL=StyleSheet HREF="css/styletest.css" TYPE="text/css">

      </head>
<body>

<div id="wrapper">

<!-- Website menu -->

        <div id="menu-bar">
            <li><a href="">Testing links</a></li>
            <li><a href="">Testing links</a></li>
            <li><a href="">Testing links</a></li>
            <li><a href="">Testing links</a></li>
            <li><a href="">Testing links</a></li>
            <li><a href="">Testing links</a></li>
        </div>  

<!-- End of Website menu -->

<div id="content">

<!-- Website header -->

        <div id="header">
            <h1>Testing header</h1>
            <?php
             if ($username && $userid) {
             echo $username; 
            } else {
            echo 'Welcome guest';
            }

            ?>
        </div>  

<!-- End of Website header -->  

<!-- -----Website content----- -->      
<h2>Log in</h2>

<?php

$form='<form action="login.php" method="POST">
            <table>
                <tr>
                    <td>Username :</td>
                    <td><input type="text" name="user"></td>
                </tr>
                <tr>
                    <td>Password :</td>
                    <td><input type="password" name="pass"></td>
                </tr>
                <tr>
                    <td></td>
                </tr>
                <tr>
                    <td></td>
                </tr>
                <tr>
                    <td></td>
                </tr>
                <tr>
                    <td></td>
                </tr>
                <tr>
                    <td><input type="submit" name="loginbtn" value="Log in"></td>
                </tr>
            </table>
        </form>';

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

if ($_POST['loginbtn']) {

    if ($user) {

        if ($pass) {

            require 'core/connect.php';

            $query = mysql_query("SELECT * FROM users WHERE username = '$user' ");
            $row = mysql_fetch_assoc($query);
            $passwordFromPost = $_POST['pass'];
            $hashedPasswordFromDB = $row['password'];

                if (password_verify($passwordFromPost, $hashedPasswordFromDB)) {

                    $query = mysql_query("SELECT * FROM users WHERE username = '$user' ");
                    $numrows = mysql_num_rows($query);
                    if ($numrows == 1) {

            $query = mysql_query("SELECT * FROM users WHERE username = '$user' ");
            $row = mysql_fetch_assoc($query);
            $dbactive = $row['active'];
            $dbuser = $row['username'];


                            if ($dbactive == 1) {

                            $_SESSION['userid'] = $dbid;
                            $_SESSION['username'] = $dbuser;    
                            echo "You have been logged in as <b>$dbuser</b> <a href='./member.php'>Click here</a> to go back to home page";


                            } else {

                            echo '<font color="red">You must activate your account to log in.</font>';
                            echo $form;

                            }

                    } else {

                    echo '<font color="red">You entered an invalid username or password.</font>';
                    echo $form;

                    }

                } else {

                echo '<font color="red">You entered an invalid username or password.</font>';   
                echo $form;

                }

            mysql_close();

        } else {

        echo '<font color="red">You must enter your password.</font>';
        echo $form;

        }

    } else {

    echo '<font color="red">You must enter your username.</font>';  
    echo $form;

    }
}else{

echo $form; 

}
 ?>

 <!-- End of Website header --> 

<!-- -----Website content----- -->      

<p>Testing website content</p>

<!-- End of Website content -->     
</div>

<!-- Website footer --> 

        <div id="footer">

        </div>

<!-- End of Website footer -->      

</div>

</body>

</html>

Now i don't think that in login.php there is any problem because i tested the page and every thing works fine even this code : 现在我不认为在login.php中有任何问题,因为我测试了该页面,并且即使此代码,一切正常:

echo "You have been logged in as <b>$dbuser</b> <a href='./member.php'>Click here</a> to go back to home page";

Where it shows him his username yet in member.php nothing happens. 在member.php中显示他的用户名的地方,什么都没有发生。 Thanks for reading. 谢谢阅读。

To me it seems the variables $username and $userid are not initialized anywhere. 在我看来,变量$username$userid并未在任何地方初始化。 Since your question involves sessions I assume you are getting those variables from the session. 由于您的问题涉及会话,我假设您从会话中获取这些变量。 If they are being declared elsewhere, the sessions are not in use (according to your code). 如果在其他地方声明了它们,则这些会话未使用(根据您的代码)。

In your member.php file you write: 在您的member.php文件中,您写道:

$_SESSION['userid']   = $dbid;
$_SESSION['username'] = $dbuser;

This seems wrong, as you are redeclaring the values in the session and not simply fetching them. 这似乎是错误的,因为您正在重新声明会话中的值而不是简单地获取它们。 Use the following instead. 请改用以下内容。

$username = isset($_SESSION['username']) ? $_SESSION['username'] : false;
$userid   = isset($_SESSION['userid'])   ? $_SESSION['userid']   : false;

This will check if the username and user id are stored in the session. 这将检查用户名和用户ID是否存储在会话中。 If they are missing the variables are assigned the value false . 如果它们丢失,则为变量赋值false Then when you further down checks the variables, the condition will fail and execute the block regarding a guest user. 然后,当您进一步检查变量时,条件将失败并执行关于访客用户的块。

Bonus 奖金

You should also make it a habit checking if variables exists before using them. 在使用变量之前,您还应该习惯检查变量是否存在。 This will help you avoid undefined variable errors. 这将帮助您避免undefined variable错误。 Use the isset keyword to check. 使用isset关键字进行检查。 This may seem like more work, but the gain in robustness of your code cannot be compared to the time you (might) have to spend fixing simple errors. 这可能看起来更像是工作,但是代码的健壮性的增加无法与您(可能)花在修复简单错误上的时间进行比较。

Hope this helps, happy coding! 希望这有帮助,快乐编码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM