搜尋
首頁php教程php手册如何用PHP生成随机验证码(附代码)

这篇文章主要讲了一个最简单的生成随机验证码图片的例子,有一定的参考价值,感兴趣的朋友可以看看。

代码如下:

<?php 
/** 默认首页 **/ 
class DefaultController extends AppController 
{ 
public function index() { 
$len = 5; 
$str = "ABCDEFGHIJKLNMPQRSTUVWXYZ123456789"; 
$im = imagecreatetruecolor ( 70, 20 ); 
$bgc = imagecolorallocate($im, 255, 255, 255); 
$bgtxt = imagecolorallocate($im, 220, 220, 220); 
//随机调色板 
$colors = array( 
imagecolorallocate($im, 255, 0, 0), 
imagecolorallocate($im, 0, 200, 0), 
imagecolorallocate($im, 0, 0, 255), 
imagecolorallocate($im, 0, 0, 0), 
imagecolorallocate($im, 255, 128, 0), 
imagecolorallocate($im, 255, 208, 0), 
imagecolorallocate($im, 98, 186, 245), 
); 
//填充背景色 
imagefill($im, 0, 0, $bgc); 
//随机获取数字 
$verify = ""; 
while (strlen($verify) < $len) { 
$i = strlen($verify); 
$random = $str[rand(0, strlen($str))]; 
$verify .= $random; 
//绘制背景文字 
imagestring($im, 6, ($i*10)+3, rand(0,6), $random, $bgtxt); 
//绘制主文字信息 
imagestring($im, 6, ($i*10)+3, rand(0,6), $random, $colors[rand(0, count($colors)-1)]); 
} 
//添加随机杂色 
for($i=0; $i<100; $i++) { 
$color = imagecolorallocate($im, rand(50,220), rand(50,220), rand(50,220)); 
imagesetpixel($im, rand(0,70), rand(0,20), $color); 
} 
//将验证码存入$_SESSION中 
sess("verify", $verify); 
//输出图片并释放缓存 
header(&#39;Content-type: image/png&#39;); 
imagepng($im); 
imagedestroy($im); 
} 
}; 
?>

【相关教程推荐】

1. php编程从入门到精通全套视频教程
2. php从入门到精通 
3. bootstrap教程

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡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脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境