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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM