检查数据库后,如何在同一表单上显示登录错误消息

时间:2013-10-15 03:35:50

标签: javascript php authentication

index.php这是我的索引文件,工作正常。做什么是检查用户会话变量并显示当前登录用户。

<?php
    error_reporting(E_ALL ^ E_NOTICE);
    session_start();
    $username = $_SESSION['username']; 
?>

   <div id="userinfo">
   <?php
      if ( $username ) {
          echo "welcome <b>$username</b> <a href='logout.php'>Logout</a>";
      } else {
          echo 'welcome guest please <a href="login.php">Login</a>';
      }
   ?> 
   </div>

loginform.php

<?php
    error_reporting(E_ALL ^ E_NOTICE);
    session_start();
    $username = $_SESSION['username'];
?>
<form id='frmlogin' method="post" action="esk.php">
    <div id="dialogcontainer">
        <div id="dialog">
           <h3>Welcome to Talk Login</h3>
           <p>Username</p>
           <input type="text" id="txtloginusername" name="txtloginusername"/>
           <p>Password</p>
           <input type="password" id="txtloginpassword" name ="txtloginpassword"/> <br /><br />
           <button type="submit">Login</button>
           <br>
           <br>
           <div id="logindisplay" class ="display"></div>
        </div>
    </div>        
 </form>

login.php而不是将消息回显给其他页面我希望它显示在loginform.php上,这恰好是我的登录表单。

 <?php
    error_reporting(E_ALL ^ E_NOTICE);
    session_start();
    $username = $_SESSION['username'];
    $user = $_POST['txtloginusername'];
    $pass = $_POST['txtloginpassword'];

    if ( $user ) {
        require ('connect.php');
        $query = mysql_query("SELECT * FROM user WHERE username = '$user'");
        $numrows = mysql_num_rows( $query );
        if ( $numrows == 1 ) {
           $row = mysql_fetch_assoc( $query );
           $dbpass = $row['password'];
           $dbuser = $row['username'];
           $dbactive = $row['active'];

           if ( $pass == $dbpass ) {
              if ( $dbactive == 1 ) {
                  $_SESSION['username'] = $dbuser;
                  header("location:index.php"); //"success";
              }
           } else {
                  echo "You did not enter the correct password"; //Display this somewhere on loginform.php
           }    
        } else {
           echo"Invalid Username"; // //Display this also in loginform.php 
        }

        mysql_close();
    } else {
        echo "Wrong username"; //this too;  
    } 

    ?>

2 个答案:

答案 0 :(得分:1)

Follow the Below Steps

1. use Ajax method to send the request from loginform.php page.

function validUser()
{   
      $.ajax({ 
      type: "POST",
      url: 'esk.php',

      data: $('#frmlogin').serialize(),

       success: function(data){
        $("#logindisplay").append(data);
       }
   });
  }

Step 2. remove submit button and change it to type button.

<input  type="button"  value="submit" onclick=validUser() />


step 3. remove the action of the form.

step 4. on esk.php page simply echo the message u want to show.

Thats It!!! 

答案 1 :(得分:-1)

您可以将错误消息传回登录表单,而不是在 login.php 中回显结果。

例如,您可以通过以下方式将“错误的用户名”传递回登录表单:

header('Location: loginform.php?err=Wrong+username');
exit;

loginform.php 中,在要显示错误的位置添加以下行:

<?php
if(isset($_GET['err'])) { echo '<p>'. $_GET['err'] . '</p>'; }
?>
相关问题