验证码验证不工作
CAPTCHA verification is NOT working
这是生成验证码图片的文件:
captcha.php
<?php
generateCaptcha();
exit();
function generateCaptcha() {
$captcha = 'ABCDEFGHJKLMNPQRSTUVWXYZ123456789abcdefghijkmnpqrstuvwxyz';
$captcha = substr(str_shuffle($captcha), 0, 6);
$_SESSION['captcha'] = $captcha; // This is NOT working!!
$font_size = 40;
$img_height = 60;
$img_width = 170;
$image = imagecreate($img_width, $img_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
imagettftext($image, $font_size, 0, 0, 50, $text_color, 'font/monofont.ttf', $captcha);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
}
?>
这是我的 index.php 文件:
<?php
require_once "includes/functions.php";
sec_session_start();
?>
<!DOCTYPE html>
<html>
<body>
<form action="process.php" method="most">
<?php echo "<img src='captcha.php'>"; ?>
<input type="text" name="captcha">
<input type="submit" name="VerifyCaptcha" value="Verify Humanship">
</form>
</body>
</html>
这是我的process.php文件:
<?php
if(isset($_SESSION['captcha'])) {
if($_SESSION['captcha'] == $_POST['captcha'])
echo "You're a human!";
else echo "I doubt you!";
} else header('Location: ../');
?>
文件 captcha.php 中的第 9 行无效。不生成会话变量。任何修复?
您可能在考虑对 $_SESSION
数组执行任何操作之前忘记调用 session_start();
。
这一行应该是执行的第一行(不完全是,但你明白了),所以我建议把它放在include require_once
指令之前,如下:
<?php
session_start();
require_once "includes/functions.php";
sec_session_start();
?>
<!DOCTYPE html>
<html>
<body>
<form action="process.php" method="most">
<?php echo "<img src='captcha.php'>"; ?>
<input type="text" name="captcha">
<input type="submit" name="VerifyCaptcha" value="Verify Humanship">
</form>
</body>
</html>
确保在 captcha.php
中执行相同的操作。
我不完全确定 sec_session_start
的作用,请尝试将该行放在 require_once
之前,看看是否可以解决问题。
终于可以解决问题了!
已更新 captcha.php
文件:
<?php
sec_session_start();
header("Pragma: no-cache");
generateCaptcha();
exit();
function generateCaptcha() {
$captcha = 'ABCDEFGHJKLMNPQRSTUVWXYZ123456789abcdefghijkmnpqrstuvwxyz';
$captcha = substr(str_shuffle($captcha), 0, 6);
$_SESSION['captcha'] = $captcha;
$font_size = 40;
$img_height = 60;
$img_width = 170;
$image = imagecreate($img_width, $img_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
imagettftext($image, $font_size, 0, 0, 50, $text_color, 'font/monofont.ttf', $captcha);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
}
function sec_session_start() {
$session_name = 'admin_panel';
session_name($session_name);
$secure = false;
$httponly = true;
if(ini_set('session.use_only_cookies', 1) === FALSE) {
header("Location: .../error.php?err=Could not initiate safe session");
exit();
}
$cookieParams = session_get_cookie_params();
session_set_cookie_params($cookieParams["lifetime"],
$cookieParams["path"],
$cookieParams["domain"],
$secure,
$httponly);
session_start();
session_regenerate_id(true);
}
?>
这是生成验证码图片的文件:
captcha.php
<?php
generateCaptcha();
exit();
function generateCaptcha() {
$captcha = 'ABCDEFGHJKLMNPQRSTUVWXYZ123456789abcdefghijkmnpqrstuvwxyz';
$captcha = substr(str_shuffle($captcha), 0, 6);
$_SESSION['captcha'] = $captcha; // This is NOT working!!
$font_size = 40;
$img_height = 60;
$img_width = 170;
$image = imagecreate($img_width, $img_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
imagettftext($image, $font_size, 0, 0, 50, $text_color, 'font/monofont.ttf', $captcha);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
}
?>
这是我的 index.php 文件:
<?php
require_once "includes/functions.php";
sec_session_start();
?>
<!DOCTYPE html>
<html>
<body>
<form action="process.php" method="most">
<?php echo "<img src='captcha.php'>"; ?>
<input type="text" name="captcha">
<input type="submit" name="VerifyCaptcha" value="Verify Humanship">
</form>
</body>
</html>
这是我的process.php文件:
<?php
if(isset($_SESSION['captcha'])) {
if($_SESSION['captcha'] == $_POST['captcha'])
echo "You're a human!";
else echo "I doubt you!";
} else header('Location: ../');
?>
文件 captcha.php 中的第 9 行无效。不生成会话变量。任何修复?
您可能在考虑对 $_SESSION
数组执行任何操作之前忘记调用 session_start();
。
这一行应该是执行的第一行(不完全是,但你明白了),所以我建议把它放在include require_once
指令之前,如下:
<?php
session_start();
require_once "includes/functions.php";
sec_session_start();
?>
<!DOCTYPE html>
<html>
<body>
<form action="process.php" method="most">
<?php echo "<img src='captcha.php'>"; ?>
<input type="text" name="captcha">
<input type="submit" name="VerifyCaptcha" value="Verify Humanship">
</form>
</body>
</html>
确保在 captcha.php
中执行相同的操作。
我不完全确定 sec_session_start
的作用,请尝试将该行放在 require_once
之前,看看是否可以解决问题。
终于可以解决问题了!
已更新 captcha.php
文件:
<?php
sec_session_start();
header("Pragma: no-cache");
generateCaptcha();
exit();
function generateCaptcha() {
$captcha = 'ABCDEFGHJKLMNPQRSTUVWXYZ123456789abcdefghijkmnpqrstuvwxyz';
$captcha = substr(str_shuffle($captcha), 0, 6);
$_SESSION['captcha'] = $captcha;
$font_size = 40;
$img_height = 60;
$img_width = 170;
$image = imagecreate($img_width, $img_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
imagettftext($image, $font_size, 0, 0, 50, $text_color, 'font/monofont.ttf', $captcha);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
}
function sec_session_start() {
$session_name = 'admin_panel';
session_name($session_name);
$secure = false;
$httponly = true;
if(ini_set('session.use_only_cookies', 1) === FALSE) {
header("Location: .../error.php?err=Could not initiate safe session");
exit();
}
$cookieParams = session_get_cookie_params();
session_set_cookie_params($cookieParams["lifetime"],
$cookieParams["path"],
$cookieParams["domain"],
$secure,
$httponly);
session_start();
session_regenerate_id(true);
}
?>