PHP實(shí)現(xiàn)變色驗(yàn)證碼實(shí)例
更新時(shí)間:2014年01月06日 17:51:16 投稿:whsnow
驗(yàn)證碼想必大家都有見到過吧,在本文為大家介紹下PHP如何實(shí)現(xiàn)變色驗(yàn)證碼,感興趣的朋友可以參考下
復(fù)制代碼 代碼如下:
<?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);
//藍(lán)色
$blue=imagecolorallocate($im, 0, 0, 255);
$color_arr=array($green,$blue,$red);
$color=array_rand($color_arr);
$textlen=iconv_strlen($text,'utf-8');//計(jì)算字符串長(zhǎng)度
//隨機(jī)截取兩個(gè)字符,變色顯示
$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不錯(cuò)的php Q扣峮:276167802,驗(yàn)證: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);
?>
驗(yàn)證碼中的字符并不是同一種顏色,讓用戶輸入指定顏色的驗(yàn)證碼,這樣安全性會(huì)更好的。
相關(guān)文章
使用bcompiler對(duì)PHP文件進(jìn)行加密的代碼
在網(wǎng)上無意間看到這個(gè)功能代碼,還沒有去試,以后有機(jī)會(huì)用到時(shí)在試一試。收藏一下。2010-08-08
php中magic_quotes_gpc對(duì)unserialize的影響分析
這篇文章主要介紹了php中magic_quotes_gpc對(duì)unserialize的影響,以實(shí)例的形式分析了magic_quotes_gpc安全過濾對(duì)unserialize造成的影響以及對(duì)此的解決方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-12-12
比較簡(jiǎn)單實(shí)用的PHP無限分類源碼分享(思路不錯(cuò))
關(guān)于PHP的無限分類代碼,網(wǎng)上已經(jīng)有很多了,講解的也很到位,在這里我分享一下我用著很順手的一個(gè)2011-10-10
PHP采用超長(zhǎng)(超大)數(shù)字運(yùn)算防止數(shù)字以科學(xué)計(jì)數(shù)法顯示的方法
這篇文章主要介紹了PHP采用超長(zhǎng)(超大)數(shù)字運(yùn)算防止數(shù)字以科學(xué)計(jì)數(shù)法顯示的方法,涉及PHP數(shù)學(xué)運(yùn)算及字符串操作的相關(guān)技巧,需要的朋友可以參考下2016-04-04

