php怎麼解決驗證碼無法顯示的問題

王林
發布: 2023-03-07 07:24:01
原創
3173 人瀏覽過

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

php怎麼解決驗證碼無法顯示的問題

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文件名 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;$n<$this->codelen;$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设计模式(部分代码如下)


        
登入後複製
l     login 
     
登入後複製

相关推荐:php培训

以上是php怎麼解決驗證碼無法顯示的問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!