我创建了一个带有验证码的简单表单(电子邮件、密码),用于登录网站。验证码(每次刷新页面随机生成)保存在
$_SESSION["captcha_code"]
中,用户编写的验证码通过表单方法post提交。
要登录网站,我们测试
$_SESSION["captcha_code"]
是否等于$_POST["captcha_code"]
。
即使测试为真也无法访问。我不知道我做错了什么。
登录.php
<?php
session_start();
?>
<section class="login-form">
<form action="includes/login.inc.php" method="post">
<input type="text" name="name" id="name" placeholder="Nom de l'utilisateur/Email...."><br><br>
<input type="password" name="pwd" id="pwd" placeholder="Mot de passe...."><br><br>
<!-- ======= Captcha Section ======= -->
<table class="table table-bordered" style="width:40%;margin:0 auto;">
<div class="form-group">
<tr>
<td colspan="3" style="width:10%;">
<img src="includes/captcha-gen.inc.php" />
</td>
<td colspan="3" style="width:10%;">
<input type="text" name="captcha_code" class="form-control" autocomplete="off" />
</td>
</tr>
</div>
</table>
<div class="form-group">
<input type="submit" id="login" name="submit" value="Connecter" class="btn btn-success" />
</div>
</form>
<?php
if (isset($_GET["error"])) {
if ($_GET["error"] == "emptyinput") {
echo "<p style='color:red;'>Remplissez tous les champs!</p>";
} else if ($_GET["error"] == "wronglogin") {
echo "<p style='color:red;'>Login incorrect!</p>";
} else if ($_GET["error"] == "incorrectCaptcha") {
echo "<p style='color:red;'>Captcha incorrect!</p>";
}
}
?>
</section>
login.inc.php
if (isset($_POST["submit"])) {
$username = $_POST["name"];
$pwd = $_POST["pwd"];
// Variable to save the captcha code sent by the form
$captcha = $_POST["captcha_code"];
// Data base connexion
require_once 'dbh.inc.php';
// page where I created the loginUser function
require_once 'functions.inc.php';
// Running error handlers and user signup
if (emptyInputLogin($username, $pwd) !== false) {
header("location: ../login.php?error=emptyinput");
exit();
// Here I made a test to compare between the captcha code saved in session and the code sent by the form
if ($captcha == $_SESSION["captcha_code"]) {
loginUser($pdo, $username, $pwd);
}
} else {
header("location: ../login.php?error=incorrectCaptcha");
}
} else {
header("location: ../login.php");
}
?>
这两个变量相等,但我无法访问并显示错误消息
Captcha incorrect!
} else {
header("location: ../login.php?error=incorrectCaptcha".'/'.$_SESSION["captcha_code"].'-'.$captcha);
}
我创建了这个测试来查看每个变量的值,这表明它们是相等的
让我们思考一下下面的逻辑:
// Running error handlers and user signup
if (emptyInputLogin($username, $pwd) !== false) {
header("location: ../login.php?error=emptyinput");
exit();
// Here I made a test to compare between the captcha code saved in session and the code sent by the form
if ($captcha == $_SESSION["captcha_code"]) {
loginUser($pdo, $username, $pwd);
}
} else {
header("location: ../login.php?error=incorrectCaptcha");
}
外层
if
检查是否emptyInputLogin($username, $pwd) !== false
。如果那是假的,那么你的验证码检查甚至不会运行,它会直接跳到你的else
,它会重定向到其他地方。
很明显,
$username
将具有form
元素的值,其name
为name
,$pwd
将具有form
元素的值,其name
为pwd
。
因为你没有分享
emptyInputLogin
和loginUser
的代码,我不确定到底会发生什么,但似乎emptyInputLogin
返回false
,因为你已经设置了name
和pwd
.如果是这样,那么修复将是
// Running error handlers and user signup
if (emptyInputLogin($username, $pwd) !== false) {
header("location: ../login.php?error=emptyinput");
exit();
} else {
// Here I made a test to compare between the captcha code saved in session and the code sent by the form
if ($captcha == $_SESSION["captcha_code"]) {
loginUser($pdo, $username, $pwd);
} else {
header("location: ../login.php?error=incorrectCaptcha");
}
}
将验证码检查移动到主
if
的其他位置,因此验证码检查只有在正确设置用户名和密码的情况下才会发生。如果验证码检查成功,则调用loginUser
,如果验证码检查失败,即验证码与预期值不匹配,则将重定向到不正确的验证码页面(login.php,错误为不正确的验证码)