PHP登录不会重定向到成员页面

时间:2012-05-20 20:28:58

标签: php html

我有PHP问题。我已经完成了一个简单的登录系统,但如果我登录它将不会重定向到成员页面,它只会转到一个空白页面。登录有效,因为我尝试了echo("success")。任何帮助将非常感激。

<?php
session_start();

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

if($username&&$password)
{
    $connect = mysql_connect("mysite","hidden","hidden") or die("Couldn't   connect");
    mysql_select_db("hidden") or die("Couldn't find db");   

    $query = mysql_query("SELECT * FROM users WHERE Username = '$username'");
    $numrows = mysql_num_rows($query);

    if($numrows!=0)
    {
        //code to login
        while($row = mysql_fetch_assoc($query))
        {
            //validate values
            $dbusername = $row['Username'];
            $dbpassword = $row['Password'];
        }
        //check to see if the values match
        if($username==$dbusername && $password==$dbpassword)
        {
            header('Location: memberHome.php');
            $_SESSION['username']=$dbusername;
        }
        else
            die("incorrect password");
    }
    else
        die("User doesn't exist");
}
else
    die("Please enter a username and a password");
?>

3 个答案:

答案 0 :(得分:1)

header('Location: memberHome.php');
$_SESSION['username']=$dbusername;

如果你想要分配会话变量,你必须在标题传递到下一页之前这样做;)像这样:

$_SESSION['username']=$dbusername;
header('Location: memberHome.php');

答案 1 :(得分:0)

你在标题后试过死吗?

$_SESSION['username']=$dbusername;
header('Location: memberHome.php');
die;

答案 2 :(得分:0)

不是先检查用户名是否存在,然后查看密码是否正确,为什么不同时进行这两种操作呢?

    $row = mysql_fetch_assoc(mysql_query("SELECT * FROM users
    WHERE Username='$username' 
    AND pass='$password'"));

if($row['id']) { 
$_SESSION['username']=$dbusername;
header('Location: memberHome.php');
}

另外,明智的做法是不要在*声明中使用SELECT。只选择你真正需要的物品。

我没有发现您的代码有任何问题,因为它没有重定向。

我只是在我的机器上使用此代码在本地尝试并取出数据库连接。但是使用下面提供的代码,我能够将它重定向到memberHome.php,这表明如果用户名和密码匹配,那么它的工作正常。

<?php
session_start();

$username = "david";
$password = "password";

if($username&&$password)
{
    //$connect = mysql_connect("mysite","hidden","hidden") or die("Couldn't   connect");
   //mysql_select_db("hidden") or die("Couldn't find db");   

    //$query = mysql_query("SELECT * FROM users WHERE Username = '$username'");
    $numrows = 1;

    if($numrows!=0)
    {

            //validate values
            $dbusername = "david";
            $dbpassword = "password";

        //check to see if the values match
        if($username==$dbusername && $password==$dbpassword)
        {
            header('Location: memberHome.php');
            $_SESSION['username']=$dbusername;
        }
        else
            die("incorrect password");
    }
    else
        die("User doesn't exist");
}
else
    die("Please enter a username and a password");
 ?>