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字符串长度计算 - strlen()函数使用介绍

strlen()函数和mb_strlen()函数 在PHP中,函数strlen()返回字符串的长度。函数原型如下: 复制代码 代码如下: int strlen(string string...

php安装swoole扩展的方法

本文实例讲述了php安装swoole扩展的方法。分享给大家供大家参考。具体如下: 我本机是OS X,想要安装swoole体验一下,于是: 复制代码 代码如下:andy@AndyMacBo...

php 远程图片保存到本地的函数类

<?php // // Function: 获取远程图片并把它保存到本地 // // // 确定您有把文件写入本地服务器的权限 // // // 变量说明: // $url 是远程...

PHP警告Cannot use a scalar value as an array的解决方法

看到php的错误日志里有些这样的提示: [27-Aug-2011 22:26:12] PHP Warning: Cannot use a scalar value as an array...

php获取通过http协议post提交过来xml数据及解析xml

php 如何获取请求的xml数据,对方通过http协议post提交过来xml数据,php如何获取到这些数据呢?复制代码 代码如下: <?php $xml_data ='<AA...