[英]How to display login error message on the same page in a php based system?
I am building a php based login system where the user is required to put in a username and password for logging in. My question is 'how can I display the 'invalid username or password' error message on the same page'? 我正在构建一个基于php的登录系统,需要用户输入用户名和密码才能登录。我的问题是“如何在同一页面上显示“无效的用户名或密码”错误消息”? I have tried many things including trying to use a query string (/?error=1);
我尝试了很多事情,包括尝试使用查询字符串(/?error = 1); it did solve my problem by displaying the error message on the same page but all the CSS was just gone from the page and I couldn't find a way to fix it (not for the lack of trying, though).
它确实通过在同一页面上显示错误消息解决了我的问题,但是所有CSS都刚刚从页面上消失了,我找不到解决它的方法(尽管如此,这不是缺乏尝试)。 So I decided to try a simpler method which, obviously, did not work.
因此,我决定尝试一种更简单的方法,该方法显然行不通。 So, how can I fix it?
那么,我该如何解决呢? Here's my PHP code:
这是我的PHP代码:
<?php
session_start();
$db = mysqli_connect("localhost","root","","combination");
if(isset($_POST['login_btn'])){
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$password=md5($password);
$sqlquery="SELECT * FROM registration WHERE username='$username' AND password='$password'";
$result = mysqli_query($db,$sqlquery);
if(mysqli_num_rows($result)==1){
$_SESSION['message']="You are now logged in.";
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
header("location:loginhome.php");
}
else{
$_SESSION['message']="Incorrect Username or Password.";
header("location:login.php");
echo '<h3>Invalid username or password</h3>';
}
}
?>
Here is a simple example to help you. 这是一个简单的示例,可以为您提供帮助。 You need to use
$_SESSION
variables. 您需要使用
$_SESSION
变量。
login.php: login.php:
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<form action="check.php" method="POST">
<input type="text" class="input" name="username" placeholder="username">
<input type="password" class="input" name="pass" placeholder="pass">
<input class="button" type="submit" id="login" name="login" value="login">
<?php
if(isset($_SESSION["error"])){
$error = $_SESSION["error"];
echo "<span>$error</span>";
}
?>
</form>
</body>
</html>
<?php
unset($_SESSION["error"]);
?>
check.php: check.php:
<?php
session_start();
$username = $_POST["username"];
$error = "username/password incorrect";
if($username == "admin"){
$_SESSION["username"] = $username;
header("location: homepage.php"); //send user to homepage, for example.
}else{
$_SESSION["error"] = $error;
header("location: login.php"); //send user back to the login page.
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.