PHP实现变色验证码实例

yipeiwu_com5年前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入门教程之表单与验证实例详解

本文实例讲述了PHP表单与验证。分享给大家供大家参考,具体如下: Demo1.php <?php ob_start(); //重新导向一个 URL //he...

PHP实现视频文件上传完整实例

本文以一个完整实例的形式实现了视频文件上传的功能。虽然是比较基础的应用,仍有一定的参考价值。分享给大家供大家参考之用。具体方法如下: 首先,对PHP来说视频也属于文件,我们利用这个道理,...

php获取文章上一页与下一页的方法

本文实例讲述了php获取文章上一页与下一页的方法。分享给大家供大家参考。具体方法如下: 今天发现一个站的上一页与下一页出现问题,上一页没有问题但是在下一页是直接到了本频道最新发布的文章了...

php使用curl伪造来源ip和refer的方法示例

本文实例讲述了php使用curl伪造来源ip和refer的方法。分享给大家供大家参考,具体如下: php curl伪造来源ip和来路refer实例代码1: //随机IP functi...

PHP获取当前日期及本周一是几月几号的方法

本文实例讲述了PHP获取当前日期及本周一是几月几号的方法。分享给大家供大家参考,具体如下: <?php header("content-type:text/html...