简体   繁体   English

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

[英]How to display login error message on the login page with php

When login is successful, the user is redirected to the main page of my site.登录成功后,用户将被重定向到我网站的主页。 My code redirects the user to a blank page when they input incorrect login details (login_parse.php), and here the error message 'Login unsuccessful' appears.当用户输入错误的登录详细信息 (login_parse.php) 时,我的代码将用户重定向到一个空白页面,并在此处显示错误消息“登录失败”。 I want the error message to appear on the login page (index.php).我希望错误消息出现在登录页面 (index.php) 上。

Login page:登录页面:

<?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: 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();
          }

}

?>

Look at http://www.phptherightway.com/#pdo_extension for input filter and PDO.查看http://www.phptherightway.com/#pdo_extension了解输入过滤器和 PDO。

You can create a session variable in else statement.您可以在 else 语句中创建会话变量。 Something Like:类似的东西:

   <?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();
     }

   }

   ?>

And in index.php check if session variabile exists and print message并在 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM