Php 帮助您在主页上显示登录错误

Php 帮助您在主页上显示登录错误,php,html,authentication,mysqli,login,Php,Html,Authentication,Mysqli,Login,我正在测试我正在构建的学习计划器的登录系统。就目前而言,该应用程序似乎允许我登录和注销,但目前的问题是,它不会在index.php(在login.php文件中初始化)上显示错误消息($error变量)。因此,我想知道是否有人能为我指出正确的方向,因为我似乎无法找出我做错了什么 我可以发布整个文件,但除了我不想淹没一个潜在的助手之外,我已经包含了我认为最重要的文件,即login.php和index.php,但如果需要,我也可以包含其他文件 先谢谢你 Login.php: <?php //

我正在测试我正在构建的学习计划器的登录系统。就目前而言,该应用程序似乎允许我登录和注销,但目前的问题是,它不会在index.php(在login.php文件中初始化)上显示错误消息(
$error
变量)。因此,我想知道是否有人能为我指出正确的方向,因为我似乎无法找出我做错了什么

我可以发布整个文件,但除了我不想淹没一个潜在的助手之外,我已经包含了我认为最重要的文件,即login.php和index.php,但如果需要,我也可以包含其他文件

先谢谢你

Login.php:

<?php

// Start session
session_start();
// Variable to store error message
$error = '';
// If the login form (Note that the 'submit' refers to the 'name' attribute of the login form) has been submitted...
if (isset($_POST['submit'])) {
    // If username or password is not provided...
    if (empty($_POST['username']) || empty($_POST['password'])) {
        // ...tell user that login details are invalid.
        $error = "Please fill in both your username and your password";
        // Else...
    } else {
        // ...put the provided username and password in variables $username and $password, respectively
        $username = $_POST['username'];
        $password = $_POST['password'];
        // Establish connection to the server
        $mysqli = mysqli_connect("localhost", "root", "");
        // set up measures to counter potential MySQL injections
        $username = stripslashes($username);
        $password = stripslashes($password);
        $username = mysqli_real_escape_string($mysqli, $username);
        $password = mysqli_real_escape_string($mysqli, $password);
        // Select Database
        $db = mysqli_select_db($mysqli, "p00702");
        // SQL query to fetch information of registerd users and find user match.
        $query = mysqli_query($mysqli, "SELECT * from logins WHERE password='$password' AND username='$username'");
        // Return the number of rows of the query result and put it in $rows variable
        $rows = mysqli_num_rows($query);
        // If rows are equal to one...
        if ($rows == 1) {
            // Initialize session with the username of the user...
            $_SESSION['login_user'] = $username;
            // ...and redirect to the homepage.
            header("Location: welcome.php");
            // Make sure that codes below do not execut upon redirection.
            exit;
        // Else, 
        } else {
            // redirect user to the home page (index.php)
            header("Location: index.php");
            // and tell user that the login credentials are invalid.
            $error = "Username or Password is invalid";
        }
        // ...and close connection
        mysqli_close($mysqli);
    }
}

解决方案1:

您可以在
edit.php的url中发送
error
,并在
index.php
上捕获错误,如下所示:

Edit.php

header("Location: index.php?error=" . $error); 
<?php echo isset($_GET['error']) ? $_GET['error'] : ""; ?>
$error = "Username or Password is invalid";
$_SESSION['error'] = $error;
// redirect user to the home page (index.php)
header("Location: index.php");
<?php echo isset($_SESSION['error']) ? $_SESSION['error'] : ""; ?>
Index.php

header("Location: index.php?error=" . $error); 
<?php echo isset($_GET['error']) ? $_GET['error'] : ""; ?>
$error = "Username or Password is invalid";
$_SESSION['error'] = $error;
// redirect user to the home page (index.php)
header("Location: index.php");
<?php echo isset($_SESSION['error']) ? $_SESSION['error'] : ""; ?>
Index.php

header("Location: index.php?error=" . $error); 
<?php echo isset($_GET['error']) ? $_GET['error'] : ""; ?>
$error = "Username or Password is invalid";
$_SESSION['error'] = $error;
// redirect user to the home page (index.php)
header("Location: index.php");
<?php echo isset($_SESSION['error']) ? $_SESSION['error'] : ""; ?>

解决方案1:

您可以在
edit.php的url中发送
error
,并在
index.php
上捕获错误,如下所示:

Edit.php

header("Location: index.php?error=" . $error); 
<?php echo isset($_GET['error']) ? $_GET['error'] : ""; ?>
$error = "Username or Password is invalid";
$_SESSION['error'] = $error;
// redirect user to the home page (index.php)
header("Location: index.php");
<?php echo isset($_SESSION['error']) ? $_SESSION['error'] : ""; ?>
Index.php

header("Location: index.php?error=" . $error); 
<?php echo isset($_GET['error']) ? $_GET['error'] : ""; ?>
$error = "Username or Password is invalid";
$_SESSION['error'] = $error;
// redirect user to the home page (index.php)
header("Location: index.php");
<?php echo isset($_SESSION['error']) ? $_SESSION['error'] : ""; ?>
Index.php

header("Location: index.php?error=" . $error); 
<?php echo isset($_GET['error']) ? $_GET['error'] : ""; ?>
$error = "Username or Password is invalid";
$_SESSION['error'] = $error;
// redirect user to the home page (index.php)
header("Location: index.php");
<?php echo isset($_SESSION['error']) ? $_SESSION['error'] : ""; ?>

您可以初始化一个空的
数组
以查找错误,并抓取其中的所有
错误
,然后通过该
数组
回送它们,例如:

$errors = [];
if(empty username or password) {
  $errors[] = 'username or email could not be empty';
}
if(username or password) {
 $errors[] = 'Username or Password is invalid';
}
// add all your conditions and get all errors
然后可以检查错误数组中是否有错误

if(count($errors)) {
  $errors_ul = '<ul>';
  foreach($errors as $error) {
    $errors_ul .= '<li>'.$error.'</li>'; 
  }

  $errors_ul .= '</ul>';

  echo $errors_ul;

} else {
  // log the user in
}
if(计数($errors)){
$errors_ul='
    '; foreach($errors作为$error){ $errors_ul.='
  • “.$error.
  • ”; } $errors_ul.='
'; echo$errors\u ul; }否则{ //让用户登录 }
您可以初始化一个空的
数组
以查找错误,并抓取其中的所有
错误
,然后通过该
数组
回音
循环
,例如:

$errors = [];
if(empty username or password) {
  $errors[] = 'username or email could not be empty';
}
if(username or password) {
 $errors[] = 'Username or Password is invalid';
}
// add all your conditions and get all errors
然后可以检查错误数组中是否有错误

if(count($errors)) {
  $errors_ul = '<ul>';
  foreach($errors as $error) {
    $errors_ul .= '<li>'.$error.'</li>'; 
  }

  $errors_ul .= '</ul>';

  echo $errors_ul;

} else {
  // log the user in
}
if(计数($errors)){
$errors_ul='
    '; foreach($errors作为$error){ $errors_ul.='
  • “.$error.
  • ”; } $errors_ul.='
'; echo$errors\u ul; }否则{ //让用户登录 }

try
var\u dump($error)
empty@FastSnail哦,是的!我想它毕竟是空的。我是PHP新手,所以我不太清楚到底发生了什么,但现在它显示:C:\wamp64\www\justread\index.PHP:48:string“”(长度=0)。这可能表明它是空的。请问您有什么想法可以纠正它吗?在某些情况下,您不会指定错误。例如,如果
($rows==1){
您是否尝试过删除这一行
标题(“Location:index.php”);
之前的
$error
变量?@FastSnail我现在会尝试并让您知道。谢谢。try
var\u dump($error)
可能是empty@FastSnail哦,是的!我想它毕竟是空的。我对PHP不太熟悉,所以我不太清楚到底发生了什么,但现在它显示:C:\wamp64\www\justread\index.PHP:48:string“”(长度=0)。这可能表明它是空的。请问您对如何更正它有何想法?在某些情况下,您不会指定错误。例如,如果
($rows==1){
您是否尝试删除这行
标题(“Location:index.php”)
$error
变量之前?@FastSnail我现在就试试,让您知道。谢谢。首先,感谢您抽出时间回复我。我尝试了所有建议,但都无效。不过,我想知道如果我在index.php文件中包含登录脚本,是否会让事情变得更简单。请建议我。首先,谢谢感谢您抽出时间回复我。我尝试了所有建议,但都不起作用。但是,我想知道如果我在index.php文件中包含登录脚本,是否会让事情变得更简单。请告诉我。首先,感谢您抽出时间回复我。我尝试了所有建议,但都不起作用。但是,我想知道它是否会起作用如果我在index.php文件中包含登录脚本,会让事情变得更简单。请告诉我。非常感谢。现在正在打印此特定错误(“用户名或密码无效”),但没有打印另一个错误(“请同时填写用户名和密码”),因此我想知道您是否认为我应该使用另一个变量,例如error2作为另一个错误?$error2=“用户名或密码无效”;$\u会话['error2']=$error2;并将其打印在索引上。页面如下:是的,您可以对其他错误使用相同的示例,但如果您想在同一位置显示错误,则可以使用相同的变量覆盖。再次感谢。希望您在需要时也能找到帮助。首先,感谢您抽出时间回复我。我尝试了所有建议,但都无效。呵呵wever,我想知道如果我在index.php文件中包含登录脚本是否会让事情变得更简单。请告诉我。非常感谢。这个特定错误(“用户名或密码无效”)现在正在打印,而不是另一个错误(“请同时填写用户名和密码”),因此我想知道您是否认为我应该使用另一个变量,例如error2作为另一个错误?$error2=“用户名或密码无效”;$\u会话['error2']=$error2;并将其打印在索引上。页面如下:是的,您可以对其他错误使用相同的示例,但如果您希望在同一位置显示错误,则可以使用相同的变量覆盖。再次感谢。如果您需要帮助,也可以寻求帮助。