以下分享一个PHP制作的图片验证码案例:案比例如以下图:

运用PHP GD具体请看:http://www.php.net/manual/zh/book.image.php

后台图片代码例如以下:

<?

php
session_start(); $image = imagecreatetruecolor(100,30);//创建一个宽100。高度30的图片
$bgcolor=imagecolorallocate($image,255,255,255);//图片背景是白色
imagefill($image,0,0,$bgcolor);//图片填充白色
//随机数
/**
for($i=0;$i<4;$i++){
$fontsize=6;
$fontcolor=imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));
$fontcontent=rand(0,9);
$x=($i*100/4)+ rand(5,10);
$y=rand(5,10);
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
**/
//随机数据
$captch_code='';
for($i=0;$i<4;$i++){
$fontsize=6;
$fontcolor=imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));
$data='asdfdfglfg74erf21854hgfhgfhkg4ljkghjtrtywiqpoqpwepdfgvnjytyut12313345645667686797800';
$fontcontent=substr($data,rand(0,strlen($data)),1);
$captch_code.=$fontcontent; $x=($i*100/4)+ rand(5,10);
$y=rand(5,10);
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
$_SESSION['authcode']=$captch_code; //随机点
for($i=0;$i<200;$i++){
$pointcolor=imagecolorallocate($image,rand(50,120),rand(50,120),rand(50,120));
imagesetpixel($image,rand(1,99),rand(1,99),$pointcolor);
}
//随机线
for($i=0;$i<3;$i++){
$linecolor=imagecolorallocate($image,rand(80,220),rand(80,220),rand(80,220));
imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$linecolor);
}
header("content-type:image/png");
imagepng($image); imagedestory($image); ?>

前台代码例如以下:

<?php
if(isset($_REQUEST['autocode'])){
session_start();
if(strtolower($_POST['autocode']) == $_SESSION['authcode']){
echo '正确';
}else{
echo'错误';
} exit();
} ?>
<!DOCTYPE html>
<html>
<head>
<meta chartset="utf-8">
</head>
<body>
<form method="post" action="form.php">
<p>验证码图片:<img border="1" src="captcha.php?r="<?php echo rand();?> width="100" height="30" /></p> <p>输入内容:<input type="text" name="autocode" value="" /></p> <p><input type="submit" value="提交" style="padding:6px 20px;"/></p> </form>
</body> </html>

加入点击运行改动例如以下就可以:

  <p>验证码图片:<img border="1" id="capthcha_img" onclick="this.src='captcha.php?r='+Math.random()" src="captcha.php?

r="<?

php echo rand();?> width="100" height="30"  /> <a href="javascript:void(0)" onclick="document.getElementById('capthcha_img').src='captcha.php?

r='+Math.random()">换一个</a></p>

本案例原创,转载请注明。

最新文章

  1. Javascript正则表达式匹配替换
  2. Xcode中使用debug模式和release模式
  3. C#如何更好地理解引用类型和值类型
  4. 【Origin】时迁念昔
  5. Duilib实现GroupBox控件
  6. POJ 1469
  7. windows的iis做后门,隐藏访问,无日志&lt;转&gt;
  8. Oracle中NVARCHAR2字符集不匹配问题
  9. Powershell访问数组
  10. Java_POI之MS-Excel2003(扩展名.xls)升级至MS-Excel2007及以上版本(扩展名.xlsx)技术过程概要
  11. 我的webstorm 使用总结
  12. MQ队列管理器搭建(三)
  13. 玩转PHP(二)--PHP强大的时间函数:date()
  14. c++析构函数调用时机
  15. reactiveCocoa使用注意点
  16. C# Socket网络编程精华篇 (转)
  17. 面面俱到的Java接口自动化测试实战
  18. 性能测试-12.Web页面性能指标与建议
  19. .netcore入门
  20. AVAudioFoundation(3):音视频编辑

热门文章

  1. Unity WebGL 窗口自适应
  2. asp.net.core网站重启后登陆无效问题(部署在IIS)
  3. Spring MVC学习总结(6)——一些Spring MVC的使用技巧
  4. HTML学习----------DAY1 第二节
  5. 【转载】C# 跨线程调用控件
  6. Windows远程登录Linux
  7. 5. webservice通信调用天气预报接口实例
  8. 如何使用通用pe工具箱破解开机密码
  9. JavaScript笔记(4)
  10. Activiti工作流框架学习(二)——使用Activiti提供的API完成流程操作