首頁  >  文章  >  後端開發  >  PHP實作變色驗證碼的方法

PHP實作變色驗證碼的方法

不言
不言原創
2018-06-21 15:20:321123瀏覽

這篇文章主要介紹了PHP實現變色驗證碼的方法,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

驗證碼想必大家都有見到過吧,在本文為大家介紹下PHP如何實現變色驗證碼,有興趣的朋友可以參考下

<?php 
header("Content-type: image/png,charset=&#39;utf-8&#39;"); 
$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); 
$text = &#39;我靠这验证码太变态啦&#39;; 
$textlen=iconv_strlen($text,&#39;utf-8&#39;);//计算字符串长度 
//随机截取两个字符,变色显示 
$p1=rand(1,$textlen)-1; 
while(($p2=rand(1,$textlen)-1)==$p1); 
$w1=iconv_substr($text,$p1,1,&#39;utf-8&#39;); 
$w2=iconv_substr($text,$p1,1,&#39;utf-8&#39;); 
//字体文件 (PS:T不错的php Q扣峮:276167802,验证:csl) 
$font = &#39;simkai.ttf&#39;; 
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,&#39;utf-8&#39;)); 
} 
else 
{ 
imagettftext($im, 15, 0, 20*($i-1)+20, 20, $black, $font, iconv_substr($text,$i,1,&#39;utf-8&#39;)); 
} 
} 
imagepng($im); 
imagedestroy($im); 
?>

驗證碼中的字元並不是同一種顏色,讓使用者輸入指定顏色的驗證碼,這樣安全性會更好的。

以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網!

相關推薦:

使用php實作加減法驗證碼

以上是PHP實作變色驗證碼的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn