为什么我要点击两次?

时间:2012-12-20 08:53:48

标签: php login

我有一个登录页面,我必须:插入用户和密码(1次);单击按钮(登录按钮)两次以实际登录。我使用Xamp与PHP 5.3和HeidiSQL。如果我已经在会话中,它会跳过登录窗口并重定向到内容页面,否则表单将被提交(这是我想要它做什么)。这是我的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
 session_start();
 include("../conect.php");



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

 { 
    $username = $_SESSION['name']; 
    $pass = $_SESSION['pass'];
    $check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());

    while($info = mysql_fetch_array( $check ))  
        {
                if ($pass != $info[2]){
                    header('.<?php $_PHP_SELF ?>.');  
                }

                else
                    {   if($_SESSION['role']==0) 
                        header("Location: content0.php");

                        if($_SESSION['role']==1) 
                        header("Location: content1.php");

                        if($_SESSION['role']==2) 
                        header("Location: content2.php");
                    }
        }
 }


 //if the login form is submitted 

 if (isset($_POST['login'])) { 
    $nume=mysql_real_escape_string($_POST['username']);
    $parola=md5(mysql_real_escape_string($_POST['pass']));
    if((!$_POST['username']) || (!$_POST['pass'])) {

        die('You did not fill in a required field.');

    }

    $check_pass = mysql_query("SELECT * FROM users WHERE username = '$nume'")or die(mysql_error());
    $check2 = mysql_num_rows($check_pass);

    if ($check2 == 0) {
        die("That user does not exist in our database");
    }

    while($data = mysql_fetch_array( $check_pass )) {
            //gives error if the password is wrong
            if ($parola != $data[2]) {

                die('Incorrect password, please try again.');
            }
            else {  
                        $result = mysql_query("SELECT * FROM users WHERE username = '$nume'")or die(mysql_error());
                        while($data=mysql_fetch_row($result)){
                            $_SESSION['name']=$data[1];
                            $_SESSION['pass']=$data[2];
                            $_SESSION['role']=$data[3];
                        }
                    }
                } 

} 


mysql_close($con);
?>

<html>

<head>
<title>Login</title>
<link rel="stylesheet" href="/css/butoane.css" type="text/css" />
<link rel="stylesheet" href="/css/admin_tools.css" type="text/css" />
<script>

</script>
</head>
<body id="login_background">

<div id="ambele">
<div class="form_box">

    <form action="<?php $_PHP_SELF ?>" method="post">
    <label for="username">Username</label><input type="text" id="username" name="username" maxlength="40"/>
    <label for="password">Password</label><input type="password" id="password"name="pass" maxlength="20"/>
    <input type="submit" name ="login" value="Login" class="button"/>
    </form>
</div>

<div id="register"> 
<a href="registration_form.php" id="reg">Not registered yet? Go to Registration</a>
</div>

</div>

</body>

</html>

1 个答案:

答案 0 :(得分:1)

我认为您需要退出脚本才能进行重定向。

<?php
header('Location: content.php');
die();
?>

此外,您将PHP标记置于错误的位置。

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

这将导致PHP中出现Headers already sent错误,因为您在重定向之前已经向客户端发送了一些内容。