Home  >  Article  >  Backend Development  >  How to implement color-changing verification code in PHP

How to implement color-changing verification code in PHP

不言
不言Original
2018-06-21 15:20:321123browse

This article mainly introduces the method of implementing color-changing verification code in PHP. It has certain reference value. Now I share it with you. Friends in need can refer to it.

Verification code must have been seen by everyone. Come on, this article will introduce to you how to implement color-changing verification codes in PHP. Interested friends can refer to

<?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); 
?>

The characters in the verification code are not the same color, allowing users to enter the verification code of the specified color. This way security will be better.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Use php to implement addition and subtraction verification codes

The above is the detailed content of How to implement color-changing verification code in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn