当前位置:Gxlcms > PHP教程 > PHP验证码图案无法显示

PHP验证码图案无法显示

时间:2021-07-01 10:21:17 帮助过:2人阅读

下面captcha.php文件运行时回报500错误, 不显示验证码图案
注掉这句"$_SESSION['pass_phrase'] = SHA($pass_phrase); "后,就不报错并且可以显示图案了,什么原因呢?
需要session保存信息要如何修改?

session_start();

// Set some important CAPTCHA constants
define('CAPTCHA_NUMCHARS', 6); // number of characters in pass-phrase
define('CAPTCHA_WIDTH', 100); // width of image
define('CAPTCHA_HEIGHT', 25); // height of image

// Generate the random pass-phrase
$pass_phrase = "";
for ($i = 0; $i < CAPTCHA_NUMCHARS; $i++) {
$pass_phrase .= chr(rand(97, 122));
}

// Store the encrypted pass-phrase in a session variable
$_SESSION['pass_phrase'] = SHA($pass_phrase);

// Create the image
$img = imagecreatetruecolor(CAPTCHA_WIDTH, CAPTCHA_HEIGHT);

// Set a white background with black text and gray graphics
$bg_color = imagecolorallocate($img, 255, 255, 255); // white
$text_color = imagecolorallocate($img, 0, 0, 0); // black
$graphic_color = imagecolorallocate($img, 64, 64, 64); // dark gray

// Fill the background
imagefilledrectangle($img, 0, 0, CAPTCHA_WIDTH, CAPTCHA_HEIGHT, $bg_color);

// Draw some random lines
for ($i = 0; $i < 5; $i++) {
imageline($img, 0, rand() % CAPTCHA_HEIGHT, CAPTCHA_WIDTH, rand() % CAPTCHA_HEIGHT, $graphic_color);
}

// Sprinkle in some random dots
for ($i = 0; $i < 50; $i++) {
imagesetpixel($img, rand() % CAPTCHA_WIDTH, rand() % CAPTCHA_HEIGHT, $graphic_color);
}
// Draw the pass-phrase string
imagettftext($img, 18, 0, 5, CAPTCHA_HEIGHT - 5, $text_color, 'Courier New Bold.ttf', $pass_phrase);
// Output the image as a PNG using a header
header('Content-type: image/png');
imagepng($img);

// Clean up
imagedestroy($img);
?>


回复讨论(解决方案)

把这句header('Content-type: image/png');拿到最前面。

将header注释掉运行看报什么错误

把这句header('Content-type: image/png');拿到最前面还是报500, 且Firefox提示:
图像"http://localhost/captcha.php"因其本身有问题无法显示

没拿到最前面时没这个提示
另外,拿到前面后,如果注掉这句"$_SESSION['pass_phrase'] = SHA($pass_phrase); ",还是可以显示的

注掉header还是报500:
GET captcha.php
500 Internal Server Error
localhost
0 B
127.0.0.1:80

响应头信息原始头信息
Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection close
Content-Length 0
Content-Type text/html
Date Sun, 03 Nov 2013 12:16:16 GMT
Expires Thu, 19 Nov 1981 08:52:00 GMT
Pragma no-cache
Server Apache/2.2.25 (Win32) PHP/5.3.27
X-Powered-By PHP/5.3.27
请求头信息原始头信息
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
Cache-Control max-age=0
Connection keep-alive
Cookie PHPSESSID=9e4i75adp4ahnpba6ieq1pinn0
Host localhost
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0

$_SESSION['pass_phrase'] = SHA($pass_phrase);
改成:
$_SESSION['pass_phrase'] = SHA1($pass_phrase);
呢?

,SHA()是MySQL的函数来着,PHP的是sha1,谢谢,给分

人气教程排行