如何使用php在登录页面上显示登录错误消息


How to display login error message on the login page with php

登录成功后,用户将重定向到我的网站主页。当用户输入错误的登录详细信息(login_parse.php)时,我的代码会将用户重定向到一个空白页面,此时会显示错误消息"登录失败"。我希望错误消息出现在登录页面(index.php)上。

登录页面:

<?php session_start(); ?>
<!DOCTYPE html> 
<html>
<link rel="stylesheet" type="text/css" href="style.css">
<head>
</head>
<body>

<form id='registration_form' action='registration.php' method='post'>
    <input id ='new_username' type='text' name='new_username'     placeholder='Create a username' oninvalid="this.setCustomValidity('Please create   a username')"required></input>
    <input id ='new_password' type='password' name='new_password'     placeholder='Create a password' oninvalid="this.setCustomValidity('Please create     a password')" required></input>
    <input id ='new_email' type='email' name='new_email' placeholder='Enter   a   valid student.lboro.ac.uk email     address'oninvalid="this.setCustomValidity('Please enter a valid     student.lboro.ac.uk email address')" required></input>
    <input id ='register_button' type='submit' value='Sign Up'/>
</form>
</body>
</html>

login_parse.php:

<?php
session_start();
include "dbconnect.php";
if (isset($_POST['username'])) {
  $username = $_POST['username'];
  $password = $_POST['password'];
  $sql = "SELECT * FROM Users WHERE username= '".$username."' AND   password='".$password."' LIMIT 1";
  $res = mysqli_query($db, $sql) or die(mysqli_error);
       if (mysqli_num_rows($res) == 1) {
          $row = mysqli_fetch_array($res);
          $_SESSION['uid'] = $row['id'];
          $_SESSION['username'] = $row['username'];
          header("Location: shownewposts.php");
          exit();

          } 
          else {
            echo "Login unsuccessful";
            exit();
          }
}
?>

查看http://www.phptherightway.com/#pdo_extension用于输入滤波器和PDO。

您可以在else语句中创建会话变量。类似:

   <?php
   session_start();
   include "connect.php";
   if (isset($_POST['username'])) {
     unset( $_SESSION['errorMessage'] );
     $username = $_POST['username'];
     $password = $_POST['password'];
     $sql = "SELECT * FROM Users WHERE username= '".$username."' AND   password='".$password."' LIMIT 1";
     $res = mysqli_query($db, $sql) or die(mysqli_error);
      if (mysqli_num_rows($res) == 1) {
         $row = mysqli_fetch_array($res);
         $_SESSION['uid'] = $row['id'];
         $_SESSION['username'] = $row['username'];
         header("Location: shownewposts.php");
         exit();
     } 
     else {
         $_SESSION['errorMessage'] = true;
         header("Location: index.php");
         exit();
     }
   }
   ?>

在index.php中检查会话变量是否存在并打印消息

    <?php session_start(); ?>
    <!DOCTYPE html> 
    <html>
    
    <link rel="stylesheet" type="text/css" href="style.css">
    
    <head>
    </head>
    
    <body>     
    
    <?php
       if (isset($_SESSION['errorMessage'])){
         echo "<span style='color:red;'>Check your input</span>";
       }
    ?>
    <form id='registration_form' action='registration.php' method='post'>
        <input id ='new_username' type='text' name='new_username'     placeholder='Create a username' oninvalid="this.setCustomValidity('Please create   a username')"required></input>
        <input id ='new_password' type='password' name='new_password'     placeholder='Create a password' oninvalid="this.setCustomValidity('Please create     a password')" required></input>
        <input id ='new_email' type='email' name='new_email' placeholder='Enter   a   valid student.lboro.ac.uk email     address'oninvalid="this.setCustomValidity('Please enter a valid     student.lboro.ac.uk email address')" required></input>
        <input id ='register_button' type='submit' value='Sign Up'/>
    </form>
    
    </body>
    </html>