新聞中心
當(dāng)我們要使用PHP進(jìn)行圖像操作的時(shí)候,必然會(huì)使用到一個(gè)PHP GD庫(kù),它是一個(gè)很強(qiáng)大的庫(kù)。今天我們要向大家介紹的就是PHP GD庫(kù)如何生成驗(yàn)證碼的相關(guān)方法。

我們提供的服務(wù)有:成都做網(wǎng)站、成都網(wǎng)站制作、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、上杭ssl等。為千余家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的上杭網(wǎng)站制作公司
#t#先在php.ini里增加一行引用:extension=php_gd2.dll
重啟apache。做一個(gè)測(cè)試頁(yè) var_dump(gd_info());輸出數(shù)據(jù)表明PHP GD庫(kù)引用成功。
表單auth.html
- http-equiv='Content-Type' content='text/html; charset=utf-8'>
驗(yàn)證碼 - 請(qǐng)輸入驗(yàn)證碼
- name="auth" type="text">
src="auth.php" border="0" />
- type="submit" value="提交">
PHP GD庫(kù)生成驗(yàn)證碼 auth.php
- session_start();
- header("Content-type:image/png");
- $img_width=100;
- $img_height=20;
- srand(microtime()*100000);
- for($i=0;$i<4;$i++)
- {
- $new_number.=dechex(rand(0,15));
- }
- $_SESSION[check_auth]=$new_number;
- $new_number=imageCreate($img_width,$img_height);//創(chuàng)建圖象
- ImageColorAllocate($new_number,255,255,255); //設(shè)置背景色為白色
- for($i=0;$i
($_SESSION[check_auth]);$i++) - {
- $font=mt_rand(3,5);
- $x=mt_rand(1,8) + $img_width*$i/4;
- $y=mt_rand(1,$img_height/4);
- $color=imageColorAllocate($new_number,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));//設(shè)置字符顏色
- imageString($new_number,$font,$x,$y,$_SESSION[check_auth][$i],$color);//輸出字符
- }
- ImagePng($new_number);
- ImageDestroy($new_number);
- ?>
PHP GD庫(kù)提交頁(yè)面 check_auth.php
- session_start();
- $auth=$_POST['auth'];
- if(empty($auth))
- {
- echo '錯(cuò)誤:驗(yàn)證碼不能為空';
- die;
- }
- if($auth==$_SESSION['check_auth'])
- {
- echo '正確';
- }
- else
- {
- echo '錯(cuò)誤:驗(yàn)證碼輸入錯(cuò)誤';
- }
- ?>
以上就是本文所介紹的PHP GD庫(kù)生成驗(yàn)證碼的相關(guān)知識(shí),希望對(duì)大家有所幫助。
文章標(biāo)題:如何運(yùn)用PHPGD庫(kù)生成驗(yàn)證碼
網(wǎng)站地址:http://m.5511xx.com/article/cdjpdpi.html


咨詢
建站咨詢
