0

这是我遇到的一个问题。我有一个 loginform.php,当用户单击登录按钮时,如果用户不存在,我想在我的“#diverror”div 上的相同“loginform.php”上显示一条错误消息。

登录表单.php

<form method="post" action="login.php">
<div style='width:500px;margin:auto;border:2px solid darkgrey;margin-top: 50px;'>
    <table id='logintable'>
        <tr>
            <td colspan="2" style='text-align: center;font-weight: bold;'>Login</td>

        </tr>

        <tr>
            <td>Username</td>
            <td><input type="text" name="username"></td>
        </tr>

        <tr>
            <td>Password</td>
            <td><input type="password" name="password"></td>
        </tr>
        <tr>
            <td></td>
            <td><button type= submit>Login</button></td>
        </tr>
    </table>
    <div id="diverror">     </div>
</div>
</form>

登录.php

<?php
    require 'connect.php';
    $username= $_POST['username'];
    $password= $_POST['password'];

        $sqlcommand = "SELECT * FROM user WHERE username = '$username'";
        $result = mysql_query($sqlcommand,$db);

        include 'loginform.php'; 
        if(mysql_num_rows($result)>=1)
        {
            $row = mysql_fetch_assoc($result);

            $dbpass = $row['password'];
            $dbuser = $row['username'];
            $dbactive = $row['active'];
            $_SESSION['username']=$dbuser;
            header ('Location: index.php');

        }
        else // If the user dose not exist,
        {
            //Display this message on the loginform.php > #diverror
        }


?>
4

2 回答 2

2

将错误消息存储在SESSION...

else // If the user dose not exist,
{
    $_SESSION['msg']="User does not exist";
    header("Location: loginform.php");
    exit();
}

然后只是回显会话变量,显示消息后您需要unset会话变量。

<div id="diverror">
   <?php echo $_SESSION['msg'];
   unset($_SESSION['msg']); ?>
</div>


注意:必须在两个文件的开头开始会话..

session_start(); 
于 2013-11-01T18:12:20.507 回答
0

为了避免在用户尚未登录的情况下出现这种情况,请使用 <div class="text-error "> <?php session_start(); if (isset($_SESSION['msg'])) { echo $_SESSION['msg']; } unset($_SESSION['msg']); ?> </div> 这种方式,只有在设置了 msg 时才会显示错误,并且只有在用户提交时才会发生这种情况。

于 2013-11-25T18:55:50.280 回答