搜尋
首頁後端開發PHP問題php驗證碼不顯示解決方法

php驗證碼不顯示解決方法

解決方法:

#1、檢查PHP是否已安裝GD擴展,並且開啟狀態;(建議學習:PHP影片教學

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

3、輸出緩衝區中的快取問題。輸出前,使用ob_clean函數解決。

4、輸出前,不能出現echo、print_r、var_dump等列印,註解或刪除解決。

解決問題之後的程式碼:

<?php
   
$w = 80; //设置图片宽和高
$h = 26;
$str = Array(); //用来存储随机码
$string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";//随机挑选其中4个字符,也可以选择更多,注意循环的时候加上,宽度适当调整
for($i = 0;$i < 4;$i++){
   $str[$i] = $string[rand(0,35)];
   $vcode .= $str[$i];
}
session_start(); //启用超全局变量session
$_SESSION["vcode"] = $vcode;
$im = imagecreatetruecolor($w,$h);
$white = imagecolorallocate($im,255,255,255); //第一次调用设置背景色
$black = imagecolorallocate($im,0,0,0); //边框颜色
imagefilledrectangle($im,0,0,$w,$h,$white); //画一矩形填充
imagerectangle($im,0,0,$w-1,$h-1,$black); //画一矩形框
//生成雪花背景
for($i = 1;$i < 200;$i++){
   $x = mt_rand(1,$w-9);
   $y = mt_rand(1,$h-9);
   $color = imagecolorallocate($im,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
   imagechar($im,1,$x,$y,"*",$color);
}
//将验证码写入图案
for($i = 0;$i < count($str);$i++){
   $x = 13 + $i * ($w - 15)/4;
   $y = mt_rand(3,$h / 3);
   $color = imagecolorallocate($im,mt_rand(0,225),mt_rand(0,150),mt_rand(0,225));
   imagechar($im,5,$x,$y,$str[$i],$color);
}
ob_clean();//原来的程序没有这一栏
header("Content-type:image/jpeg"); //以jpeg格式输出,注意上面不能输出任何字符,否则出错
imagejpeg($im);
imagedestroy($im);


?>

顯示結果如下:

php驗證碼不顯示解決方法

#

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

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境