拓展
我們需要開啟gd拓展旱函,可以使用下面的代碼來查看是否開啟gd拓展盒揉。
<?php
echo "Hello World!!!!";
echo phpinfo();
?>
然后在瀏覽器上Ctrl+F
查找gd選項即可驗證自己有沒有裝這個拓展缆八,如果沒有的話胞谭,還需要自己全裝一下這個拓展恨溜。
背景圖
imagecreatetruecolor
默認生成黑色背景
<?php
// 使用gd的imagecreatetruecolor();創(chuàng)建一張背景圖
$image = imagecreatetruecolor(100,30);
// 在顯示這張圖片的時候一定要先聲明頭信息
header('content-type:image/png');
imagepng($image);
// 釋放資源符衔,銷毀執(zhí)行對象
imagedestroy($image);
imagecolorallocate
創(chuàng)建一個填充色,并用imagefill(image糟袁,x,y,color)方法來附著判族。
<?php
// 使用gd的imagecreatetruecolor();創(chuàng)建一張背景圖
$image = imagecreatetruecolor(100,30);
// 生成填充色
$bgcolor = imagecolorallocate($image,255,255,255);
// 將填充色填充到背景圖上
imagefill($image,0,0,$bgcolor);
// 在顯示這張圖片的時候一定要先聲明頭信息
header('content-type:image/png');
imagepng($image);
// 釋放資源,銷毀執(zhí)行對象
imagedestroy($image);
imagepng
在使用這個方法之前项戴,一定要先設(shè)置頭信息形帮,否則不會正常的顯示圖片
imagedestory(image)
適時的釋放資源會減輕對服務(wù)器請求的壓力。
簡易數(shù)字驗證碼
imagecolorallocate
生成顏色信息周叮,方便待會的賦予處理辩撑。
$fontcolor=imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));
imagestring
把內(nèi)容信息寫到圖片的相應(yīng)位置上。
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
增加識別干擾
增加點
// 生成一些干擾的點仿耽,這里是200個
for($i=0;$i<200;$i++){
$pointcolor = imagecolorallocate($image,rand(50,255),rand(50,255),rand(50,255));
imagesetpixel($image,rand(0,100),rand(0,30),$pointcolor);
}
增加線
// 生成一些干擾線 這里是5個
for($i=0;$i<5;$i++){
// 設(shè)置為淺色的線合冀,防止喧賓奪主
$linecolor = imagecolorallocate($image,rand(50,255),rand(50,255),rand(50,255));
imageline($image,rand(0,99),rand(0,29),rand(0,99),rand(0,29),$linecolor);
}
數(shù)字字母混合驗證碼
<?php
// 使用gd的imagecreatetruecolor();創(chuàng)建一張背景圖
$image = imagecreatetruecolor(100,40);
// 生成填充色
$bgcolor = imagecolorallocate($image,255,255,255);
// 將填充色填充到背景圖上
imagefill($image,0,0,$bgcolor);
//////// 生成隨機4位字母以及數(shù)字混合的驗證碼
for($i=0;$i<4;$i++){
$fontsize = rand(6,8);
$fontcolor = imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));
// 為了避免用戶難于辨認,去掉了某些有歧義的字母和數(shù)字
$rawstr = 'abcdefghjkmnopqrstuvwxyz23456789ABCDEFGHJKLMNOPQRSTUVWXYZ';
$fontcontent = substr($rawstr,rand(0,strlen($rawstr)),1);
// 避免生成的圖片重疊
$x += 20;
$y = rand(10,20);
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
// 生成一些干擾的點项贺,這里是200個
for($i=0;$i<200;$i++){
$pointcolor = imagecolorallocate($image,rand(50,255),rand(50,255),rand(50,255));
imagesetpixel($image,rand(0,100),rand(0,30),$pointcolor);
}
// 生成一些干擾線 這里是4個
for($i=0;$i<4;$i++){
// 設(shè)置為淺色的線君躺,防止喧賓奪主
$linecolor = imagecolorallocate($image,rand(50,255),rand(50,255),rand(50,255));
imageline($image,rand(0,99),rand(0,29),rand(0,99),rand(0,29),$linecolor);
}
header('content-type:image/png');
imagepng($image);
// 釋放資源峭判,銷毀執(zhí)行對象
imagedestroy($image);
使用驗證碼
開啟session的時機
注意: 開啟session一定要在開始的地方
驗證的原理
驗證的過程就是客戶端輸入的驗證碼和存在于session域中的驗證碼進行對比。即:
if(isset($_REQUEST['checkcode'])){
session_start();
if($_REQUEST['checkcode']==$_SESSION['checkcode']){
echo "<font color='green'>Success!</font>";
}else{
echo "<font color='red'>Failed!</font>";
}
exit();
}
優(yōu)化驗證
但是簡單的這樣驗證有一點不好的地方棕叫,那就是字母的大小寫容易出錯林螃。所以我們要做一下轉(zhuǎn)換,將用戶輸入的數(shù)值全部變成小寫的俺泣。
if(strtolower($_REQUEST['checkcode'])==$_SESSION['checkcode']){···}
小案例
生成驗證碼
<?php
session_start();// 必須在php的最開始部分聲明治宣,來開啟session
// 使用gd的imagecreatetruecolor();創(chuàng)建一張背景圖
$image = imagecreatetruecolor(100,40);
// 生成填充色
$bgcolor = imagecolorallocate($image,255,255,255);
// 將填充色填充到背景圖上
imagefill($image,0,0,$bgcolor);
//////// 生成隨機4位字母以及數(shù)字混合的驗證碼
$checkcode='';
for($i=0;$i<4;$i++){
$fontsize = rand(6,8);
$fontcolor = imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));
// 為了避免用戶難于辨認,去掉了某些有歧義的字母和數(shù)字
$rawstr = 'abcdefghjkmnopqrstuvwxyz23456789';
$fontcontent = substr($rawstr,rand(0,strlen($rawstr)),1);
// 拼接即將誕生的驗證碼
$checkcode.=$fontcontent;
// 避免生成的圖片重疊
$x += 20;
$y = rand(10,20);
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
// 保存到session變量中
$_SESSION['checkcode']=$checkcode;
// 生成一些干擾的點砌滞,這里是200個
for($i=0;$i<200;$i++){
$pointcolor = imagecolorallocate($image,rand(50,255),rand(50,255),rand(50,255));
imagesetpixel($image,rand(0,100),rand(0,30),$pointcolor);
}
// 生成一些干擾線 這里是4個
for($i=0;$i<4;$i++){
// 設(shè)置為淺色的線侮邀,防止喧賓奪主
$linecolor = imagecolorallocate($image,rand(50,255),rand(50,255),rand(50,255));
imageline($image,rand(0,99),rand(0,29),rand(0,99),rand(0,29),$linecolor);
}
header('content-type:image/png');
imagepng($image);
// 釋放資源,銷毀執(zhí)行對象
imagedestroy($image);
表單驗證
<?php
header("Content-Type:text/html;charset=utf8");
if(isset($_REQUEST['checkcode'])){
session_start();
if(strtolower($_REQUEST['checkcode'])==$_SESSION['checkcode']){
echo "<font color='green'>Success!</font>";
}else{
echo "<font color='red'>Failed!</font>";
}
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>驗證驗證碼信息</title>
<script>
function change(){
document.getElementById("image_checkcode").src='./store.php?r='+Math.random();
}
</script>
</head>
<body>
<form action="./form.php" method="post">
<p>驗證碼圖片:</p><img id="image_checkcode" src="./store.php?r=<?php echo rand();?>" /><a href="javascript:void(0)" onclick="change()">看不清楚</a><br/>
請輸入驗證碼<input type="text" name="checkcode" /><br />
<p><input type="submit" value="提交" /></p>
</form>
</body>
</html>
總結(jié)
最后贝润,來個總結(jié)吧绊茧。
- 使用php制作驗證碼需要gd拓展的支持。
- 使用imagecreatetruecolor方法生成背景色打掘,并用imagefill填充一個由imagecolorallocate產(chǎn)生的顏色华畏。
- 使用imagestring來實現(xiàn)驗證碼和背景圖的結(jié)合
- 使用imagesetpixel來添加干擾點
- 使用imageline來添加干擾線
- 使用session之前要在開頭開啟session_start()方法
- 使用JavaScript來動態(tài)的修改驗證碼的src,來滿足用戶“換一張”的需求尊蚁。