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();

转载于:https://www.cnblogs.com/tsg0727/p/11061069.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<?php header('content-type:text/html;charset=utf-8'); //登录界面 require 'ljsql.php';//连接数据库 //判断表单是否提交,用户名密码是否提交 if (isset($_POST['username'])&&isset($_POST['password'])){//登录表单已提交 //获取用户输入的验证码 $captcha = isset($_POST['captcha']) ? trim($_POST['captcha']) : ''; //获取Session中的验证码 session_start(); if(empty($_SESSION['captcha'])){ //如果Session中不存在验证码,则退出 exit('验证码已经过期,请返回并刷新页面重试。'); } //获取验证码并清除Session中的验证码 $true_captcha = $_SESSION['captcha']; unset($_SESSION['captcha']); //限制验证码只能验证一次,防止重复利用 //忽略字符串的大小写,进行比较 if(strtolower($captcha) !== strtolower($true_captcha)){ exit('您输入的验证码不正确!请返回并刷新页面重试。'); } //验证验证通过,继续判断用户名和密码 //获取用户输入的用户名密码 $username=$_POST["username"]; $pwd=$_POST["password"]; $sql="select id,username,password from user where username='$username' and password='$pwd';"; $result=mysqli_query($con, $sql);//执行sql语句 $row=mysqli_num_rows($result);//返回值条目 if (!$row){//若返回条目不存在则证明该账号不存在或者密码输入错误 echo "<script>alert('账号不存在或密码错误,点击前往注册');location='./register.php'</script>"; //exit('账号或密码错误'); }else{//存在返回条目证明用户账号密码匹配,进入主页面 session_start(); $_SESSION['username']=$_POST['username']; echo "<script>alert('欢迎{$username}');location='../cwyy/index.html'</script>"; } }登录页面为什么登录不进去
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值