php生成验证码图片
0:效果图
1:index.php用来生成验证码图片
<?php
session_start();
header ('Content-Type: image/png');
$image=imagecreatetruecolor(100, 30);
//背景颜色为白色
$color=imagecolorallocate($image, 255, 255, 255);
imagefill($image, 20, 20, $color);
// for($i=0;$i<4;$i++){
// $font=6;
// $x=rand(5,10)+$i*100/4;
// $y=rand(8, 15);
// $string=rand(0, 9);
// $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
// imagestring($image, $font, $x, $y, $string, $color);
// }
$code='';
for($i=0;$i<4;$i++){
$fontSize=8;
$x=rand(5,10)+$i*100/4;
$y=rand(5, 15);
$data='abcdefghijklmnopqrstuvwxyz123456789';
$string=substr($data,rand(0, strlen($data)),1);
$code.=$string;
$color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
imagestring($image, $fontSize, $x, $y, $string, $color);
}
$_SESSION['code']=$code;//存储在session里
for($i=0;$i<200;$i++){
$pointColor=imagecolorallocate($image, rand(100, 255), rand(100, 255), rand(100, 255));
imagesetpixel($image, rand(0, 100), rand(0, 30), $pointColor);
}
for($i=0;$i<2;$i++){
$linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
}
imagepng($image);
imagedestroy($image);
?>
2:form.php前端页面.用于输入验证码并验证
<?php
if(isset($_REQUEST['code'])){
session_start();
if($_REQUEST['code']==$_SESSION['code']){
echo "<font color='red'>输入正确</font>";
}else{
echo "<font color='red'>输入错误</font>";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta chartset="UTF-8" />
<style type="text/css" rel="stylesheet">
a{
text-decoration:none;
font-size:30px;
color:blue;
}
a:hover{
text-decoration:underline;
}
</style>
</head>
<body>
<form action="form.php" method="get">
验证码: <img id="img" src="index.php?" onclick="changeCode()"/>
<a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
请输入验证码: <input name="code" /><br />
<input type="submit" value="提交" />
</form>
</body>
<script type="text/javascript"> function changeCode(){
var img=document.getElementById('img');
//img.src='index.php?r='+Math.random();
img.setAttribute('src','index.php?r='+Math.random());
}
</script>
</html>
php生成验证码图片的更多相关文章
- java web学习总结(九) -------------------通过Servlet生成验证码图片
一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:
- JavaWeb---总结(九)通过Servlet生成验证码图片
一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下: 创建一个DrawImage Servlet,用来生成验证码图片 1 package gacl. ...
- Java 生成验证码图片
生成验证码图片并对提交的输入进行验证 // HttpServletResponse常见应用——生成验证码 // 利用BufferedImage类生产随机图片 public static final i ...
- javaweb学习总结(九)—— 通过Servlet生成验证码图片
一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:
- 012. asp.net生成验证码图片(汉字示例/字母+数字)
protected void Page_Load(object sender, EventArgs e) { //生成验证码图片的基本步骤 string checkCode = "新年快乐& ...
- J2EE如何生成验证码图片和点击刷新验证码
验证码图片生成步骤 创建BufferedImage对象. 获取BufferedImage的画笔,即调用getGraphics()方法获取Graphics对象. 调用Graphics对象的setColo ...
- java web 学习九(通过servlet生成验证码图片)
一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:
- java web,生成验证码图片的技术
偶然知道原来有些网站的验证码图片都是随机生成的,后来听人讲了一下,就做了这个小例子 生成图片,绘制背景,数字,干扰线用到了java.awt包,主要使用BufferedImage来生成图片,然后使用Gr ...
- 通过Servlet生成验证码图片
原文出自:http://www.cnblogs.com/xdp-gacl/p/3798190.html 一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类, ...
随机推荐
- WordPress Suco Themes ‘themify-ajax.php’任意文件上传漏洞
漏洞名称: WordPress Suco Themes ‘themify-ajax.php’任意文件上传漏洞 CNNVD编号: CNNVD-201311-403 发布时间: 2013-11-28 更新 ...
- 基于WebForm+EasyUI的业务管理系统形成之旅 -- 数据统计(Ⅳ)
上篇<基于WebForm+EasyUI的业务管理系统形成之旅 -- 首页快捷方式>,主要介绍通过添加首页快捷方式,快速进入各个应用菜单功能. 将常用的菜单功能作为快捷方式,避免由于寻找诸多 ...
- c#打印机设置,取得打印机列表及相应打印机的所有纸张格式
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- linux c遍历文件夹 和文件查找的方法
linux c遍历文件夹的方法比较简单,使用c来实现 #include <iostream> #include <stdio.h> #include <sys/types ...
- NOIP2015 斗地主(搜索+剪枝)
4325: NOIP2015 斗地主 Time Limit: 30 Sec Memory Limit: 1024 MBSubmit: 270 Solved: 192[Submit][Status] ...
- Windows Azure的故障检测和重试逻辑
高度可用的应用程序设计的一个关键点,是利用代码中的重试逻辑正常处理临时中断的服务.Microsoft 模式和实践团队开发的暂时性故障处理应用程序块可协助应用程序开发人员完成此过程.“暂时性”一词表示仅 ...
- 各种sensor名称统计
gyroscopes 陀螺仪accelerometers 加速度计magnetometers 磁力计barometric pressure 气压remote pressure sensing 远程压力 ...
- Weka 自动优化参数
import weka.core.*; import weka.classifiers.*; import weka.classifiers.meta.*; import weka.classifie ...
- 【Java基础】继承的一些总结
什么是继承 把一些类的具有共性的东西剥离出来形成一个新的类,然后各个其他类保留自己独有的特性,并用关键字extends继承这个剥离出来的新的类,可以最终达到各类原始相同效果,但是在每个类中,单用一个“ ...
- ehcache在windows下使用RMI同步时出现的问题
问题 最近的项目由于OP分配的机器没有配Redis或Mem,为了解决tomcat的集群问题,项目使用了Ehcache作为缓存.Ehcache使用RMI进行同步,但在本地测试时出现了缓存未同步的问题.最 ...