Chinaunix首页 | 论坛 | 博客
  • 博客访问: 154288
  • 博文数量: 42
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 399
  • 用 户 组: 普通用户
  • 注册时间: 2015-09-23 11:47
个人简介

程序猿啊程序猿

文章分类

全部博文(42)

文章存档

2016年(28)

2015年(14)

我的朋友

分类: PHP

2015-12-10 17:25:12


点击(此处)折叠或打开

  1. <?php
  2. session_start();//启动新会话
  3. for($i=0;$i<4;$i++){
  4.     @$codestring .= strtoupper(dechex(mt_rand(0,15)));
  5. } // 随机一个若干位验证码,并将它们转换为十六进制并大写输出!
  6. $_SESSION['code']=$codestring;//设置全局会话参数,方便验证码在各PHP页面中调用
  7. $width=75;
  8. $height=25;
  9. $image=imagecreatetruecolor($width, $height);//创建固定长宽的图片
  10. $white=imagecolorallocate($image,255,255,255);//给一副图像分配颜色
  11. imagefill($image,0,0,$white);//区域填充
  12. $flag=false;
  13. if($flag){
  14. $color=imagecolorallocate($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));//给一副图像分配颜色
  15. imagerectangle($image,0,0,$width-1,$height-1,$color);//画一个矩形并颜色填充
  16. }
  17. for($i=0;$i<17;$i++){
  18.     $color=imagecolorallocate($image,mt_rand(100,255),mt_rand(150,255),mt_rand(200,255));//给一副图像分配颜色
  19.     imageline($image,mt_rand(0,$width),mt_rand(0,$height),
  20.         mt_rand(0,$width),mt_rand(0,$height),$color);//随机画出若干条直线,并给他们分配颜色
  21. }
  22. for($i=0;$i<100;$i++){
  23.     $color=imagecolorallocate($image,mt_rand(100,255),mt_rand(150,255),mt_rand(200,255));//给一副图像分配颜色
  24.     imagestring($image,1,mt_rand(0,$width),mt_rand(0,$height),'*',$color);//给图片加上随机位置字符串并颜色填充
  25. }
  26. for($i=0;$i<strlen($_SESSION['code']);$i++){
  27.     $color=imagecolorallocate($image,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));//给一副图像分配颜色
  28.     imagestring($image,5,($i+1)*$width/4-mt_rand(8,15),
  29.         mt_rand(0,$height/2),$_SESSION['code'][$i], $color);//给图片加上已经确定好大致位置内的随机位置的字符串并颜色填充
  30. }
  31. header('Content-Type:image/png');//标明图片类型
  32. imagepng($image);//输出PNG图片
  33. imagedestroy($image);//销毁图片
  34. ?>

PS:在同一目录下新建reg.php文件,敲打以下代码,即可调用验证码图片和实现点击图片刷新验证码,代码如下:

点击(此处)折叠或打开

  1. <?php
  2. session_start();
  3. ?>
  4. <img src="code.php" onclick="javascript:this.src='code.php?tm='+Math.random()"/>

阅读(1571) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~