PHP实现变色验证码实例

yipeiwu_com6年前PHP代码库
复制代码 代码如下:

<?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);
$text = '我靠这验证码太变态啦';
$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);
?>

验证码中的字符并不是同一种颜色,让用户输入指定颜色的验证码,这样安全性会更好的。

相关文章

php访问数组最后一个元素的函数end()用法

本文实例讲述了php访问数组最后一个元素的函数end()用法。分享给大家供大家参考。具体分析如下: end()函数在PHP中用于检索数组中的最后一个元素。end()函数需要一个数组作为其...

PHP实现桶排序算法

简单意义上的桶排序: 桶排序的原理是先安排N+1个桶作为容器,若数据范围为N的话。 然后将测试数据(所需排序的数据)进行循环,放入对应的桶内。数据一定是在范围N内的。 最后,循环桶里的元...

php获取文件类型和文件信息的方法

本文实例讲述了php获取文件类型和文件信息的方法。分享给大家供大家参考。具体实现方法如下: <?php $file = "php.txt"; //打开文件,r表示以只读...

基于magic_quotes_gpc与magic_quotes_runtime的区别与使用介绍

当你的数据中有一些   \  ”  ‘ 这样的字符要写入到数据库里面,又想不被过滤掉的时候,它就很有用,会在这些字符前自动加上\,如中国\地大物博...

PHP经典的给图片加水印程序

<?php   /**************************************************************  参数...