当输入错误的密码时,它将不会返回index.html,它只会返回到loggedin.html。请帮忙
<!DOCTYPE html><?php session_start();?>
<html>
<head>
<title>User Login</title>
</head>
<body>
<form action="login.php" method="post">
<table width="500" align="center" bgcolor="skyblue">
<tr align="center">
<td colspan="3"><h2>User Login</h2></td>
</tr>
<tr>
<td align="right"><b>Email</b></td>
<td><input type="text" name="email" required="required"/></td>
</tr>
<tr>
<td align="right"><b>Password:</b></td>
<td><input type="password" name="pass" required="required"></td>
</tr>
<tr align="center">
<td colspan="3">
<input type="submit" name="login" value="Login">
</td>
</tr>
</table>
</form>
</body>
</html>
这是我的index.php
<!DOCTYPE html>
<?php
// establishing the MySQLi connection
$con = mysqli_connect("localhost","root","usbw","users");
if (mysqli_connect_errno())
{
echo "MySQLi Connection was not established:" . mysqli_connect_error();
}
// checking the user
if(isset($_POST['login'])){
$email = mysqli_real_escape_string($con,$_POST['email']);
$pass = mysqli_real_escape_string($con,$_POST['pass']);
$sel_user = "SELECT * FROM users WHERE user_email='$email' AND user_pass='$pass'";
$run_user = mysqli_query($con, $sel_user);
$check_user = mysqli_num_rows($run_user);
if($check_user> 1){
$_SESSION['user_email']=$email;
echo "<script>window.open('loggedin.html','_self')</script>";
}
else {
echo "<script>window.open('index.html', '_self') </script>";
}
}
?>
这是我的login.php
当输入错误的密码时,它将不会返回index.html,它只会返回到loggedin.html。请帮忙
答案 0 :(得分:2)
尝试:
if($check_user >= 1) or if($check_user > 0)
而不是
if($check_user > 1)
因为根据您的请求,我认为您只选择了一位用户,因此,如果您选择> 1
,则必须选择最少2位用户。
另外,要重定向,请参阅header
方法。例如:header('Location: loggedin.html');
会将您重定向到“已登录”状态。页。
你必须在所有其他事情之前使用session_start()
。因此,在定义session_start
doctype
答案 1 :(得分:0)
使用header("Location: ")
重定向,但请确保您之前不输出任何其他HTML,例如<!DOCTYPE html>
标记或任何空格。我绝对不会在这里使用javascript重定向。
答案 2 :(得分:0)
这是一个登录表单,因此只有一个用户名和密码匹配是可以接受的。肯定num_rows
应该为1。
if ($check_user == 1) {
$_SESSION['user_email'] = $email;
header('Location: loggedin.html');
}
else {
header('Location: index.html');
}
答案 3 :(得分:-1)
你可以在下面使用它将起作用
$sel_user = "SELECT * FROM users WHERE user_email='".$email."' AND user_pass='".$pass."'";
if($check_user > 0)