php.ini 验证码,php怎么解决验证码无法显示的问题

php解决验证码无法显示的问题的方法:1、检查php代码,确保代码全部正确;2、编辑php.ini配置文件,修改【extension = php_gd2.dll】;3、清除缓存区;4、重新启动apache服务器。

797b98bfa7fa12778b5b481ed603878e.png

PHP解决验证码无法显示的方法:

(推荐教程:php视频教程)

第一步:先确保代码没有写错

第二步:确保gd扩展库的开启

在php.ini在设置:extension=php_gd2.dll

第三步:清除缓存区

PHP关于缓存区的三个函数

ob_get_contents() - 返回输出缓冲区的内容

ob_flush() - 冲刷出(送出)输出缓冲区中的内容

ob_clean() - 清空(擦掉)输出缓冲区

ob_end_flush() - 冲刷出(送出)输出缓冲区内容并关闭缓冲

ob_end_clean() - 清空(擦除)缓冲区并关闭输出缓冲

flush() - 刷新输出缓冲

第四步:重新启动Apache服务器并再次运行

代码实现://Captcha.class.php文件名

//定义最终的图像验证码类

final class Captcha{

//私有的成员属性

private $code;//验证码字符串

private $codelen;//验证码长度

private $width; //画布宽度

private $height; //画布高度

private $img; //图像资源

private $fontsize; //字号大小

private $fontfile; //字体文件

//构造方法:对象初始化

public function __construct($codelen=4,$width=100,$height=36,$fontsize=20)

{

$this->codelen = $codelen;

$this->width = $width;

$this->height = $height;

$this->fontsize = $fontsize;

$this->fontfile = ".\\Public\\Home\\Fonts\\msyh.ttc";

$this->code = $this->createCode();

$this->img = $this->createImg();

$this->createBg(); //给画布添加背景色

$this->createText(); //写入字符串

$this->line(); //增加干扰线

$this->outPut(); //输出图像

}

//私有的生成验证码随机字符串

private function createCode()

{

//产生随机的字符串数组

$arr_str = array_merge(range('a', 'z'),range('A', 'Z'),range(0,9));

//打乱数组

shuffle($arr_str);

shuffle($arr_str);

//从数组中随机指定个数下标

$arr_index = array_rand($arr_str,$this->codelen);

//循环下标数组,构建随机字符串

$str ="";

foreach ($arr_index as $i) {

$str .=$arr_str[$i];

}

//将验证码字符串存入sess

$_SESSION['captcha'] = $str;

return $str;

}

//私有的创建一个空画布

private function createImg()

{

return imagecreatetruecolor($this->width, $this->height);

}

//私有的分配画布背景色

private function createBg()

{

$bgcolor = imagecolorallocate($this->img, mt_rand(0,255), mt_rand(0,200), mt_rand(100,255));

//绘制带背景的矩形

imagefilledrectangle($this->img, 0, 0,$this->width,$this->height, $bgcolor);

}

//私有的写入验证码字符串

private function createText()

{

//给文本分配颜色

$color = imagecolorallocate($this->img, mt_rand(100,255), mt_rand(0,200), mt_rand(100,200));

//写入字符串

imagettftext($this->img, $this->fontsize, -12, 10, 20, $color, $this->fontfile, $this->code);

//imagestring($this->img, $this->fontsize, 8, 8, $this->code, $color);

}

//私有的输出图像

private function line()

{

//增加干扰线

for($n=1;$ncodelen;$n++){

$linecolor = imagecolorallocate($this->img, mt_rand(0,200), mt_rand(0,200), mt_rand(0,200));

imageline($this->img,mt_rand(0,80), mt_rand(0,30), mt_rand(0,100), mt_rand(0,30), $linecolor);

}

}

//公共的输出图像

private function outPut()

{

//声明输出的内容的类型

header("content-type:image/png");

//输出图像

imagepng($this->img);

//销毁图像资源

imagedestroy($this->img);

}

}

MVC设计模式(部分代码如下)<?php

//LoginController.class.php文件名

//定义最终的登录控制器类

final class LoginController{

//创建验证码类对象

public function captcha(){

ob_clean();// 清空(擦掉)输出缓冲区

//新建Captcha类对象;由于Captcha类只有一个公共的构造方法;新建对象时对调用构造方法里面传递的所有方法;

$obj=new Captcha();

}

}l

login

?p=Home&c=Login&a=captcha

相关推荐:php培训

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值