登录表单不起作用

时间:2014-11-30 14:23:15

标签: php login

我编写了一个登录表单,每个人都可以工作(连接到db,查询,结果)。但是我想要显示登录表单!我应该根据$countResult变量来决定。如果它等于0,则显示Form!如果是1则不显示表单然后重定向到其他页面。

$countResult一直工作到// it is working up to the here。为什么呢?

<?php
session_start();
$_SESSION["username"] = "";
$_SESSION["userGroup"] = "";


require_once 'db.php';

$username ;
$usergroup ;
$loginError = "";
$usernameError = "";
$passwordError = "";
$query = "SELECT usergroup FROM personnel";
$result = "";
$queryResult = '';
static $countResult = 0 ;
$connectionLink = connectToDB();
$selectedDB = selectDB("ghiasvan" , $connectionLink);
static $showForm = True ;

if($_SERVER["REQUEST_METHOD"] == "POST"){
    //global $showForm, $countResult;
    if(!empty($_POST["username"]))
        $username = trim($_POST["username"]);
    else
        $usernameError = "fill username";   

    if(!empty($_POST["password"]))
        $password = trim($_POST["password"]);
     else
        $passwordError = "fill passowrd";

    //echo password_hash($password, salt);
    if(empty($password) && empty($username))
        $loginError = "<b>fill username and password<b/>" ;

    if(empty($passwordError) && empty($usernameError)){

        $query .= " WHERE username = '$username' and password = '$password' ";
        $queryResult = databaseQuery($query);
        $countResult = mysql_num_rows($queryResult);
        if($countResult == 0) {
        $showForm = TRUE;
        }
        else if ($countResult == 1) {
            $showForm = False;
            $row = mysql_fetch_row($queryResult);
            $usergroup = $row[0] ;
            $_SESSION["userGroup"] = $usergroup;
        }
    }
} // end of if submit $_POST


?>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <?php 
        global $countResult;
        if($countResult == 0){
            $showForm = TRUE;

        } else if ($countResult == 1) {
            $showForm = FALSE;
        }       
        if ($showForm){
            echo "<title>Login Form</title>";
            echo '<link href="/cafecalendar/css/login.css" rel="stylesheet" type="text/css" />';
        }

    ?>



// it is working up to the here  


</head>
<body>
<?php           
if ( $countResult == 0 ){

?>

<div id="divForm">
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST">
    <div id="inputPos">
        Username :<br /><input type="text" name="username" id="username" /><br />
        Password :<br /><input type="password" name="password" id="password" /><br />
                    <input type="submit" value="Login" name="submit" id="submit"><br />
                    <?php  
                        if($loginError){
                            echo $loginError;
                        } else{
                            echo $usernameError . $passwordError;
                        }
                    ?>
    </div>          
</form>
</div> <!-- end of div form -->
<?php 
} // end of showing loggin form
else if ($countResult == 1){
if($usergroup == 1){

    header("Location: http://localhost/ghias/admin.php");
    die();
}
else if ($usergroup == 2){
    header("Location: http://localhost/ghias/teacher.php");
    die();
}
else {
    header("Location: http://localhost/ghias/admin.php");
    die();
}
}
?>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

我认为您应该检查表单是否已提交,请尝试这样:

<?php if(isset($_POST['submit'])):?>
    <!--show your form here-->
<?php endif; ?>