我正在嘗試創建驗證碼,但正在創建的代碼未存儲在會話變量中。這裏是我用來創建代碼並存儲值的文件。我在我的php頁面上有一個session_start(),它也會顯示驗證碼。存儲驗證碼的會話值
<?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);
?>
我可以在會話中存儲其他項目,他們工作得很好。我不確定爲什麼這個值不會存儲。我發現有關獲取captcha工作的其他信息,但沒有提到在會話中存儲值的問題。有什麼建議麼?
感謝 羅伯特
如果這是用於生產代碼,您是否看過[reCAPTCHA](http://www.google.com/recaptcha)? – Austin 2011-05-07 19:32:09
我已經遇到了與我的網站之一相同的問題,奇怪的是我有它的工作約10個網站,這一個不保存會話..你可以評論輸出線(頭+圖片+ imagedestroy)和print_r($ _ SESSION)來查看運行文件時是否存儲它。 – 2011-05-07 19:32:52
謝謝奧斯汀,我會研究一下。這就是我跑進Pendo的原因,我不確定這是否與爲圖像值設置標題有關,或者發生了什麼,但似乎有些奇怪,它可以在某些地方使用,但不是這個。 – Robert 2011-05-07 19:35:04