在member.php页面会话输出“隐含的用户名”在login.php中工作,但不在member.php中显示

时间:2013-01-14 21:50:16

标签: php

在member.php页面会话输出中的php问题“隐含的用户名”在login.php中有效但在member.php中没有显示

<html>

<form action="login.php" method="POST">
    Username: <input type="text" name="username"><p>
    Password: <input type="password" name="password">
              <input type="submit" name="submit" value="Login">
</form>

<a href='register'.php>Register Now</a>

</html
上面的

代码是index.html文件

<?php

session_start();

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

if ($username&&$password)
    {

        $connect = mysql_connect("127.0.0.1","root","") or die ("Could not connect `to     database");
        mysql_selectdb("login") or die ("could not find database");
        $query = mysql_query("select * FROM users WHERE username='$username'");
        $numrows = mysql_num_rows($query);
        if($numrows !=0)
    {
    while ($row = mysql_fetch_assoc($query))
    {
        $dbusername = $row['username'];
        $dbpassword = $row['password'];

    }
    if ($username==$dbusername&&$password==$dbpassword)
    {
    echo $_Session['username']="$dbusername  ";
    echo ",  Login successful. <a href='member.php'>Click here to enter the Members area  

</a>";




    }
    else
        echo "Incorrect password";
    }
    else
        die ("That username does not exists");
    }
    else
        die ("Please enter a username and password");

?>  
上面的

代码是login.php,在登录页面上显示“username”登录成功,点击此处的链接转到会员区。

<?php

session_start();

if ($_SESSION['username'] = '$dbusername')
    {
  echo "Welcome, ".$_SESSION['username']."<br><<a href='logout.php'>Click here</a> to logout!<br>Click<a href='changepassword.php'> here</a> to change your password!";
    }
else
     die("You must be logged in to see this page");
上面的

是来自member.php页面的代码

这是显示输出:

欢迎,$ dbusername

点击此处退出! 点击此处更改密码!

我的问题是它应该如下所示:

欢迎,约翰尼 点击这里退出! 点击此处更改密码!

解决方案会很棒!

1 个答案:

答案 0 :(得分:0)

从哪里开始?好的,这不会作为安全的登录脚本。首先,您将密码以原始形式存储在数据库中,不加密。接下来检查是否存在会话变量不是查看用户是否已登录的正确方法。每次更改页面时都必须检查用户凭据,并且最好重新生成会话ID。

$dbusername在第二页上没有任何价值。您在使用=符号时将$_SESSION['username']重置为$dbusername

shouldBe这样:

if ($_SESSION['username']) 

if (isset($_SESSION['username']))

这是hashing the password

的好地方

Please, don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial