这是生成验证码图像的文件:
的 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行无效。不生成会话变量。任何修复?
答案 0 :(得分:1)
在考虑使用session_start();
数组做任何事情之前,您可能忘记致电$_SESSION
。
这一行应该是执行的第一行(不完全是,但是你明白了),所以我建议在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
之前,看看是否能修复它。
答案 1 :(得分:0)
终于能够解决问题了!
更新了<?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);
}
?>
文件:
BaseView