0

我在一个网站上工作,我从数据库中获取记录以匹配用户输入,如果数据与数据库中的详细信息不匹配,它必须显示一个 javascript 警报框。但问题是 javascript 框也显示在身体负载上。以下是我的代码:

//Storing the value of form's username and password into PHP variables
$username = $_POST['username'];
$password = $_POST['password'];
//This query is to check whether ther user has provided valid details
$query = mysql_query("Select * from accounts where username='$username' and     password='$password'");
$status = mysql_num_rows($query);
/**Now checking whether the number of rows returned by database are greater than 0 to  verify the login
if number of rows are greater than zero we will redirect the user to index page with a     session variable**/

if($status >0)
{
$_SESSION['username'] = $username;
?>
<meta http-equiv="refresh" content="0;url=index.php">
<?}
else
{?>
<script language="javascript">
alert('UserName or Password does not match. Please try again');
</script>
<?}?>
4

3 回答 3

1

代替

else

else if (isset($_POST['username'])) {

如果您不希望在用户未尝试登录时出现警报。

于 2012-11-09T12:47:21.560 回答
0

为此,您需要一个登录标志。

将此添加到您的表单中:

<input type="hidden" name="login_process" value="true" />

然后用以下内容包围您的登录代码:

if($_POST["login_process"] == "true"){ 

    //your login code

}

...your page body
于 2012-11-09T12:45:37.543 回答
0
you miss <?php and Varible
    this Code Use then Check
       <?php 

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

            //This query is to check whether ther user has provided valid details
            $query = mysql_query("Select * from accounts where username='".$username."' and     password='**".$password."**'");
            $status = mysql_num_rows($query);

            /**Now checking whether the number of rows returned by database are greater than 0 to  verify the login if number of rows are greater than zero we will redirect the user to index page with a session variable**/

            if($status >0)
            {
               $_SESSION['username'] = $username;
            ?>
            <meta http-equiv="refresh" content="0;url=index.php">
            **<?php** }
            else
            {?>
            <script language="javascript">
            alert('UserName or Password does not match. Please try again');
            </script>
            **<?php** }?>
于 2012-11-09T12:49:02.397 回答