保持用户在浏览网站时登录

时间:2013-03-25 22:48:32

标签: php

当用户登录我的网站时,login.php会检查他们是否拥有正确的用户名密码,或者他们是否为管理员:

session_start ();

$username = '';
$password = '';
$dbusername = '';
$dbpassword = '';
if (isset($_POST['Email']) && isset($_POST['Password']))
{
    $username = $_POST['Email'];
    $password = md5($_POST['Password']);



$query = mysql_query("SELECT * FROM member WHERE Email ='$username' AND Password='$password'");


$numrow = mysql_num_rows ($query);
// user login
if ($numrow!=0)
{
    while ($row = mysql_fetch_assoc($query))
    {
        $dbusername = $row['Email'];
        $dbpassword = $row['Password'];
    }

    //Check to see if they match
    if ($username==$dbusername&&$password==$dbpassword)
    {
        header("Location: member.php");
        $_SESSION ['Email']=$username;

    }
    }
    else 
    {
        // admin login
        $query2 = mysql_query("SELECT * FROM admin WHERE Email ='$username' AND Password ='$password'");
        $numrow2 = mysql_num_rows ($query2);
        if ($numrow2!=0)
        {
            while ($row = mysql_fetch_assoc($query2))
            {
                $dbusername = $row['Email'];
                $dbpassword = $row['Password'];
            }

            //Check to see if they match
          if ($username==$dbusername&&$password==$dbpassword)
            {
                header("Location: admin.php");
                $_SESSION ['Email']=$username;
            }
            else{

                echo "Incorrect password";
            }

        }
            else{
        if ($username!=$dbusername&&$password!=$dbpassword)
        {die("That user does not exist!");
        }
        }
    }
}

它们被重定向到member.php(下面的相关代码)

session_start ();

     If (logged_in() === true)//Email 
           echo "Welcome, ".$_SESSION['Email']. "!<br><ahref='logout.php'>Logout</a>";
     else
    die ("You must be logged in");

一切正常,用户登录并且他们的用户名显示在页面顶部,但如果用户返回主页或网站上的任何其他页面,则他们不再登录。完全混淆怎么做,任何帮助都会很棒。

1 个答案:

答案 0 :(得分:1)

您需要在重定向用户之前设置会话变量