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中的static和yield关键字

前言 本文主要给大家介绍了关于PHP中static和yield关键字的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。 先来说说 static 关键字。本篇只讲...

php基于环形链表解决约瑟夫环问题示例

本文实例讲述了php基于环形链表解决约瑟夫环问题。分享给大家供大家参考,具体如下: 先来重温一下约瑟夫环问题:N个人围成一圈,从第一个开始报数,第M个将被杀掉,最后剩下一个,其余人都将被...

比较详细PHP生成静态页面教程

一,PHP脚本与动态页面。   PHP脚本是一种服务器端脚本程序,可通过嵌入等方法与HTML文件混合,也可以类,函数封装等形式,以模板的方式对用户请求进行处理。无论以何种方式,它的基本原...

php+ajax登录跳转登录实现思路

当我们的用户进行系统登录时,用户名和密码的验证都是后端验证的。而且,用户登录状态也是要后端设置的,查询数据库后,用户名和密码正确,则在session中存储一个uuid,每个页面需要根据登...

PHP封装的完整分页类示例

本文实例讲述了PHP封装的完整分页类。分享给大家供大家参考,具体如下: <?php class pageclass{ private $total; //总记...