我正在使用这个脚本:https://github.com/claviska/simple-php-captcha
在 php 中制作验证码。
我有这个代码:
session_start();
include_once 'simple-php-captcha.php';
$_SESSION = array();
$_SESSION['captcha'] = simple_php_captcha();
$code = $_SESSION['captcha']['code'];
if (isset($_POST['username'])&&isset($_POST['password'])) {
if (!empty($_POST['username']) && !empty($_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
if (isset($_POST['captcha'])&&!empty($_POST['captcha'])) {
$captcha = $_POST['captcha'];
if ($captcha == $code) {
echo "Login";
} else {
$errors[] = "Capcha is Not True.";
}
} else {
$errors[] = "Enter capcha.";
}
} else {
$errors[] = "Enter all.";
}
}
和 HTML 表单:
<form accept="" method="POST">
username: <input type="text" name="username">
password: <input type="password" name="password">
captcha: <input type="text" name="captcha">
<img src="<?php echo $_SESSION['captcha']['image_src']; ?>">
<input type="submit" class="Button" value="submit">
</form>
但是
问题是每次我输入验证码并提交表单时,都会生成较新的验证码并且表单总是返回 NOT TRUE CAPTCHA pm。
例如我打开页面,例如验证码是:52111, (我输入正确)然后我提交,然后我看到验证码不正确的错误,因为生成的较新的验证码是另一个像:48852。
我的意思是每次页面生成一个更新的页面,我该如何解决这个问题??
最佳答案
在检查表单之前,您应该保存旧的验证码值:
session_start();
if (!isset($_SESSION['captcha']) {
$_SESSION['captcha'] = simple_php_captcha();
}
$code = $_SESSION['captcha']['code'];
include_once 'simple-php-captcha.php';
if (isset($_POST['username'])&&isset($_POST['password'])) {
if (!empty($_POST['username']) && !empty($_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
if (isset($_POST['captcha'])&&!empty($_POST['captcha'])) {
$captcha = $_POST['captcha'];
if ($captcha == $code) {
echo "Login";
} else {
$errors[] = "Capcha is Not True.";
}
unlink($_SESSION['captcha'])
} else {
$errors[] = "Enter capcha.";
}
} else {
$errors[] = "Enter all.";
}
}
关于php Captcha 正在返回旧值并检查旧值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25663454/