PHP验证码图案无法显示

php中文网
发布: 2016-06-23 14:26:26
原创
1283人浏览过

下面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      $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      imageline($img, 0, rand() % CAPTCHA_HEIGHT, CAPTCHA_WIDTH, rand() % CAPTCHA_HEIGHT, $graphic_color);
  }

  // Sprinkle in some random dots
  for ($i = 0; $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,谢谢,给分

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号