首页 > 资讯列表 > 编程/数据库 >> PHP

PHP实现变色验证码实例代码

PHP 2014-12-24 21:30:08 转载来源: 网络整理/侵权必删

本文为大家讲解的是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 = '我靠这验证码

本文为大家讲解的是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 实现 变色 验证 实例 代码


声明:本文内容来源自网络,文字、图片等素材版权属于原作者,平台转载素材出于传递更多信息,文章内容仅供参考与学习,切勿作为商业目的使用。如果侵害了您的合法权益,请您及时与我们联系,我们会在第一时间进行处理!我们尊重版权,也致力于保护版权,站搜网感谢您的分享!

站长搜索

http://www.adminso.com

Copyright @ 2007~2024 All Rights Reserved.

Powered By 站长搜索

打开手机扫描上面的二维码打开手机版


使用手机软件扫描微信二维码

关注我们可获取更多热点资讯

站长搜索目录系统技术支持