您当前的位置: 首页 > 网站编程 > PHP教程 > PHP实现变色验证码实例

PHP实现变色验证码实例

作者:不详 来源:网络 发布时间: 2014-08-10 21:43 点击:
验证码想必大家都有见到过吧,在本文为大家介绍下PHP如何实现变色验证码,感兴趣的朋友可以参考下 代码如下: ?php header(Content-type: image/png,charset='utf-8'); $im = imagecreatetruecolor(400, 30); //白色 $white = imagecolorallocate($im, 255, 255, 255);

PHP实现变色验证码实例

  验证码想必大家都有见到过吧,在本文为大家介绍下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实现变色验证码实例"由远航站长收集整理而来,仅供大家学习与参考使用。更多网站制作教程尽在远航站长站。
顶一下
(0)
0%
踩一下
(0)
0%
[点击 次] [返回上一页] [打印]
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
关于本站 - 联系我们 - 网站声明 - 友情连接- 网站地图 - 站点地图 - 返回顶部
Copyright © 2007-2013 www.yhzhan.com(远航站长). All Rights Reserved .
远航站长:为中小站长提供最佳的学习与交流平台,提供网页制作与网站编程等各类网站制作教程.
官方QQ:445490277 网站群:26680406 网站备案号:豫ICP备07500620号-4