PHP 验证码不显示问题排查

1、检查PHP是否已安装GD扩展,并且开启状态;

2、utf-8 BOM头原因。用Editplus、ultraedit,删除即可。(https://blog.csdn.net/oscar999/article/details/6280006)

3、输出缓冲区中的缓存问题。输出前,使用ob_clean函数解决。

4、输出前,不能出现echo、print_r、var_dump等打印,注释或删除 解决。
---------------------
作者:一统云起
来源:CSDN
原文:https://blog.csdn.net/u013829518/article/details/80533656
版权声明:本文为博主原创文章,转载请附上博文链接!

 

 

 

class Captcha {
var $captcha_width = 70; // 文件上传路径 结尾加斜杠
var $captcha_height = 25; // 缩略图路径(必须在$images_dir下建立) 结尾加斜杠

/**
* +----------------------------------------------------------
* 构造函数
* +----------------------------------------------------------
*/
function Captcha($captcha_width, $captcha_height) {
$this->captcha_width = $captcha_width;
$this->captcha_height = $captcha_height;
}

/**
* +----------------------------------------------------------
* 图片上传的处理函数
* +----------------------------------------------------------
*/
function create_captcha() {
// 随机四位数字和大写字母组合
$word = strtoupper($GLOBALS['dou']->create_rand_string('letter.number', 4));

// 把验证码字符串写入session
$_SESSION['captcha'] = md5($word . DOU_SHELL);

// 绘制基本框架
$im = imagecreatetruecolor($this->captcha_width, $this->captcha_height);
$bg_color = imagecolorallocate($im, 235, 236, 237);
imagefilledrectangle($im, 0, 0, $this->captcha_width, $this->captcha_height, $bg_color);
$border_color = imagecolorallocate($im, 118, 151, 199);
imagerectangle($im, 0, 0, $this->captcha_width - 1, $this->captcha_height - 1, $border_color);

// 添加干扰
for($i = 0; $i < 5; $i++) {
$rand_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagearc($im, mt_rand(-$this->captcha_width, $this->captcha_width), mt_rand(-$this->captcha_height, $this->captcha_height), mt_rand(30, $this->captcha_width * 2), mt_rand(20, $this->captcha_height * 2), mt_rand(0, 360), mt_rand(0, 360), $rand_color);
}
for($i = 0; $i < 50; $i++) {
$rand_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagesetpixel($im, mt_rand(0, $this->captcha_width), mt_rand(0, $this->captcha_height), $rand_color);
}

// 生成验证码图片
$text_color = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
imagestring($im, 6, 18, 5, $word, $text_color);

// header
header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate");
header("Content-type: image/png;charset=utf-8");

/* 绘图结束 */
imagepng($im);
imagedestroy($im);

return true;
}
}


调用方法:
// 开启SESSION
session_start();

// 实例化验证码
$captcha = new Captcha(70, 25);

// 清除之前出现的多余输入
@ob_end_clean();
@ob_clean();

$captcha->create_captcha();
posted @ 2019-06-20 20:10  笑~颜  阅读(1470)  评论(0编辑  收藏  举报