Php 存储验证码的会话值';s
我正在尝试创建验证码,但是正在创建的代码没有存储在会话变量中。这是我用来创建代码和存储值的文件。我的php页面上有一个session_start(),它也会显示验证码Php 存储验证码的会话值';s,php,session,captcha,Php,Session,Captcha,我正在尝试创建验证码,但是正在创建的代码没有存储在会话变量中。这是我用来创建代码和存储值的文件。我的php页面上有一个session_start(),它也会显示验证码 <?php session_start(); class CaptchaSecurityImages { var $font = './monofont.ttf'; function generateCode($characters) { /* list all possible characters, simi
<?php session_start();
class CaptchaSecurityImages {
var $font = './monofont.ttf';
function generateCode($characters) {
/* list all possible characters, similar looking characters and vowels have been removed */
$possible = '23456789bcdfghjkmnpqrstvwxyz';
$code = '';
$i = 0;
while ($i < $characters) {
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
$i++;
}
return $code;
}
function CaptchaSecurityImages($width='120',$height='40',$characters='6') {
$code = $this->generateCode($characters);
/* font size will be 75% of the image height */
$_SESSION['security_code'] = $code;
$font_size = $height * 0.75;
$image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
/* set the colours */
$background_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 20, 40, 100);
$noise_color = imagecolorallocate($image, 100, 120, 180);
/* generate random dots in background */
for( $i=0; $i<($width*$height)/3; $i++ ) {
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
}
/* generate random lines in background */
for( $i=0; $i<($width*$height)/150; $i++ ) {
imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
}
/* create textbox and add text */
$textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
$x = ($width - $textbox[4])/2;
$y = ($height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
/* output captcha image to browser */
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
}
}
$width = isset($_GET['width']) ? $_GET['width'] : '120';
$height = isset($_GET['height']) ? $_GET['height'] : '40';
$characters = isset($_GET['characters']) && $_GET['characters'] > 1 ? $_GET['characters'] : '6';
$captcha = new CaptchaSecurityImages($width,$height,$characters);
?>
我可以在会话中存储其他项目,它们工作得很好。我不确定为什么这个值不能存储。我还发现了其他关于验证码工作的帖子,但没有关于在会话中存储值的问题。有什么建议吗
谢谢
Robert如何将您的图像加载到用户的浏览器中?它在同一个域上吗?如果不是,问题很可能是您的cookie被排除在外,因为它是“第三方cookie”。要让浏览器保存此cookie,您必须设置一个标题,以便浏览器知道可以保存cookie 有效的P3P标头示例如下:
header('P3P: CP="CAO PSA OUR"');
希望这能有所帮助。我最终确实使用了reCAPTCHA来完成这项任务。如果这是用于生产代码,你有没有研究过?我的一个网站也遇到了同样的问题,奇怪的是,我已经在大约10个网站上使用了它,而这个网站并没有保存会话。。您可以在运行文件时对输出行(header+imagejpg+imagedestroy)和print_r($_SESSION)进行注释,以查看是否存储了该文件。感谢Austin,我将对此进行研究。这就是我遇到的Pendo,我不确定这是否与设置图像值的标题有关,或者是发生了什么事情,但似乎奇怪的是,它在某些地方可以工作,但在这个地方不行。你能添加你用来显示的代码吗?这是我下载代码的URL。即使使用下载中的确切文件也不起作用。正在将映像加载到同一域中。它是从包含文件加载的,会对会话的使用产生任何影响吗?这不会有任何影响。问题似乎是,当请求映像时,您的客户端没有发送当前的PHP会话id。您确实有
session_start()
位于输出HTML以加载图像的文档顶部,对吗?是会话_start()位于创建图像的页面顶部。否,不是创建图像的页面。我看你有。我们需要它在第1页上。显示用户正在填写的表单(或您正在执行的任何操作)和2。在处理用户输入的页面顶部(提交验证码后)。如果它不在这两个位置,那么您没有得到$\u会话['security\u code']
的原因是会话没有持续通过您的所有页面。是的会话\u start()位于图像创建页面的顶部,用户填写的表单也是提交表单的页面。因此有两个页面,都有session_start()。