PHP實現(xiàn)變色驗證碼實例
更新時間:2014年01月06日 17:51:16 投稿:whsnow
驗證碼想必大家都有見到過吧,在本文為大家介紹下PHP如何實現(xiàn)變色驗證碼,感興趣的朋友可以參考下
復制代碼 代碼如下:
<?php
header("Content-type: image/png,charset='utf-8'");
$im = imagecreatetruecolor(400, 30);
//白色
$white = imagecolorallocate($im, 255, 255, 255);
//紅色
$red = imagecolorallocate($im, 255, 0, 0);
//黑色
$black=imagecolorallocate($im, 0, 0, 0);
//綠色
$green=imagecolorallocate($im, 0, 255, 0);
//藍色
$blue=imagecolorallocate($im, 0, 0, 255);
$color_arr=array($green,$blue,$red);
$color=array_rand($color_arr);
$textlen=iconv_strlen($text,'utf-8');//計算字符串長度
//隨機截取兩個字符,變色顯示
$p1=rand(1,$textlen)-1;
while(($p2=rand(1,$textlen)-1)==$p1);
$w1=iconv_substr($text,$p1,1,'utf-8');
$w2=iconv_substr($text,$p1,1,'utf-8');
//字體文件 (PS:T不錯的php Q扣峮:276167802,驗證:csl)
$font = 'simkai.ttf';
imagefilledrectangle($im, 0, 0, 399, 29, $white);
for($i=0;$i<$textlen;$i++)
{
if($i==$p1||$i==$p2)
{
imagettftext($im, 15, 0, 20*($i-1)+20, 20, $color_arr[$color], $font, iconv_substr($text,$i,1,'utf-8'));
}
else
{
imagettftext($im, 15, 0, 20*($i-1)+20, 20, $black, $font, iconv_substr($text,$i,1,'utf-8'));
}
}
imagepng($im);
imagedestroy($im);
?>
驗證碼中的字符并不是同一種顏色,讓用戶輸入指定顏色的驗證碼,這樣安全性會更好的。
相關(guān)文章
php中關(guān)于codeigniter的xmlrpc的類在進行數(shù)據(jù)交換時的類型問題
在使用codeigniter的xmlrpc類進行客戶端/服務端請求應答的時候,客戶端需要發(fā)送請求參數(shù)給服務端,服務端在接受到參數(shù)之后進行參數(shù)分析,分拆參數(shù)之后分配給正確的方法進行處理,處理之后反饋一個response給客戶端。2011-07-07PHP數(shù)組相加操作及與array_merge的區(qū)別淺析
這篇文章主要給大家介紹了關(guān)于PHP數(shù)組相加操作以及與array_merge的區(qū)別,文中通過示例介紹的很詳細,感興趣的朋友們可以參考學習,有需要的下面跟著小編一起來學習學習吧。2016-11-11一些PHP Coding Tips(php小技巧)[2011/04/02最后更新]
以下是一些PHP Coding Tips. 當然, 這些Tips并不一定僅僅局限于PHP.大家有什么好的心得, 歡迎直接回復本文與更多的人分享.. 謝謝2011-05-05php頁碼形式分頁函數(shù)支持靜態(tài)化地址及ajax分頁
這篇文章主要介紹了php頁碼形式分頁函數(shù),此分頁支持靜態(tài)化地址分頁和無鏈接地址時的ajax分頁,需要的朋友可以參考下2014-03-03一款簡單實用的php操作mysql數(shù)據(jù)庫類
這篇文章主要介紹了一款簡單實用的php操作mysql數(shù)據(jù)庫類,不但包含了php針對mysql數(shù)據(jù)庫的常見操作之外,還有針對危險字符的過濾功能,非常具有實用價值,需要的朋友可以參考下2014-12-12