我有问题 这是我的index.php文件
<section class="main">
<form class="form-3" method="post" action="back/login.php">
<p class="clearfix">
<input type="text" name="username" id="username" placeholder="username">
<label for="login">username</label>
</p>
<p class="clearfix">
<input type="password" name="password" id="password" placeholder="password">
<label for="password">password</label>
</p>
<p class="clearfix">
<input type="submit" name="submit" value="Sign In">
</p>
<p class="clearfix">
<label for="remember">remember me</label>
<input type="checkbox" name="remember" id="remember">
</p>
</form>
*
<div class="error"><?php echo $error;?></div>
*
</section>
这是我的login.php文件
<?php
session_start();
include("connection.php");
$error = "";
if (isset($_POST["submit"])) {
if (empty($_POST["username"]) || empty($_POST["password"]))
$error = "Both fields are required.";
else {
$username = $_POST['username'];
$password = $_POST['password'];
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysqli_real_escape_string($db, $username);
$password = mysqli_real_escape_string($db, $password);
$password = md5($password);
$sql = "SELECT uid FROM users WHERE username='$username' and password='$password'";
$result = mysqli_query($db,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$login_user = $username;
if (mysqli_num_rows($result) == 1) {
$_SESSION['username'] = $login_user;
header("location: home.php");
}
else {
$error = "Incorrect username or password.";
header("location: ../index.php");
}
}
}
但是html代码中的$ error错误无效...
我的意思是当我输入错误的密码和用户名时,它只是重定向我的index.php页面,里面没有任何错误......
我的login.php文件也包含在我的index.php
中答案 0 :(得分:1)
这是因为 $ error 变量位于 login.php 文件中,不是index.php 。
index.php 文件中基本上没有名为$ error的变量。
执行您要执行的操作的一种简单方法是从 login.php 重定向到 index.php ,并使用以下错误变量:
header("location: ../index.php?error=1");
然后在index.php中,使用switch case来表示错误:
if(isset($_GET['error'])) {
$error_id = $_GET['error'];
switch($error_id) {
case 1:
$error = "Incorrect username or password.";
break;
}
}
我希望有所帮助!