登录成功后,用户将被重定向到我网站的主页面。我的代码在输入错误的登录详细信息(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();
}
}
?>
答案 0 :(得分:0)
查看http://www.phptherightway.com/#pdo_extension输入过滤器和PDO。
您可以在else语句中创建会话变量。 喜欢的东西:
<?php
session_start();
include "dbconnect.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'] = 1;
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>