PHP验证码
来源:互联网 发布:司令部升级数据 编辑:程序博客网 时间:2024/05/17 02:12
<?php
Header("Content-type: image/PNG");
//初始化
$border = 1; //是否要边框 1要:0不要
$how = 5; //验证码位数
$w = $how*13; //图片宽度
$h = 20; //图片高度
$fontsize = 5; //字体大小
$array="0123456789"; //随机字符
$seccode = ""; //验证码字符串初始化
srand((double)microtime()*1000000); //初始化随机数种子
$im = ImageCreate($w, $h); //创建验证图片
//绘制基本框架
$bgcolor = ImageColorAllocate($im, 255, 255, 255); //设置背景颜色
ImageFill($im, 0, 0, $bgcolor); //填充背景色
if($border) {
$black = ImageColorAllocate($im, 0, 0, 0); //设置边框颜色
ImageRectangle($im, 0, 0, $w-1, $h-1, $black); //绘制边框
}
//逐位产生随机字符
for($i=0; $i<$how; $i++) {
$code = substr($array,rand(0,strlen($array)-1),1); //取字符
$j = !$i ? 4 : $j+12; //绘字符位置
$color3 = ImageColorAllocate($im, mt_rand(0,100), mt_rand(0,100), mt_rand(0,100)); //字符随即颜色
ImageChar($im, $fontsize, $j, 3, $code, $color3); //绘字符
$seccode .= $code; //逐位加入验证码字符串
}
//添加干扰
for($i=0; $i<5; $i++) { //绘背景干扰线
$color1 = ImageColorAllocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)); //干扰线颜色
ImageArc($im, mt_rand(-5,$w), mt_rand(-5,$h), mt_rand(20,300), mt_rand(20,200), 55, 44, $color1); //干扰线
}
for($i=0; $i<$how*40; $i++){ //绘背景干扰点
$color2 = ImageColorAllocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)); //干扰点颜色
ImageSetPixel($im, mt_rand(0,$w), mt_rand(0,$h), $color2); //干扰点
}
//把验证码字符串写入session
session_start();
$_SESSION['seccode'] = $seccode;
/*绘图结束*/
Imagegif($im);
ImageDestroy($im);
/*绘图结束*/
?>
验证页面
<?php
session_start();
$type=$_POST['type'];
if($type=="seccode") {
if($_SESSION['seccode']==$_POST['SEC']) {
echo "验证码正确";
} else {
echo "验证码错误";
}
} elseif($type=="Username") {
if($_POST['SEC']=="a") {
echo "用户名正确";
} else {
echo "用户名错误";
}
} elseif($type=="Password") {
if($_POST['SEC']=="b") {
echo "密码正确";
} else {
echo "密码错误";
}
}
?>
调用
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
.checkcode
{
cursor: pointer;
}
</style>
<script type="text/javascript" src="cc.js"></script>
<script type="text/javascript">
function check(pp,type) {
Ajax.sendurl="d.php?authnum="+Math.random();
Ajax.method="post";
Ajax.contentid="Err";
Ajax.loadingid="Err";
Ajax.loading='数据加载中,请稍候...'
Ajax.sendstr="type="+type+"&SEC="+document.getElementById(pp).value;
Ajax.send();
}
function seccode() {
check("SEC","seccode");
}
function username() {
check("USer","Username");
}
function password() {
check("Pass","Password");
}
</script>
</head>
<body>
<div id="Err"></div>
<br />
<img src="c.php?authnum=123456789" onclick="this.src='c.php?authnum='+Math.random()"
alt="如果不认识点击图片刷新" class="checkcode" /><br />
<input type="text" id="SEC" onblur="seccode();" />
<button id="SD" onclick="seccode();">
Check</button>
<br />
<input type="text" id="USer" onblur="username();" />
<button id="SD" onclick="username();">
Check</button>
<input type="text" id="Pass" onblur="password();" />
<button id="SD" onclick="username();">
Check</button>
</body>
</html>
- php图形验证码
- php 验证码
- PHP 验证码
- PHP图片验证码
- php验证码类
- php实现验证码
- PHP实现验证码
- PHP实现验证码
- PHP生成验证码
- PHP验证码
- php验证码类
- PHP 生成验证码
- php 验证码
- php 图片验证码
- php 验证码控件
- php 验证码总结
- PHP验证码生成
- PHP生成验证码
- 姓氏的联想
- 软考冲刺2008 系统分析版 V1.0发布!
- 风, 大风
- 开张喽!!!
- [web开发] apache+mod_perl实现url rewrite
- PHP验证码
- (STL)for_each、find_if用法
- 微软商业智能BI解决方案
- 思科 7300 系列路由器
- asp.net 实现多语言
- 怎样把字母转换成二进制数
- 围棋,又见围棋
- 不吃早餐可能损害心脏
- 二进制转换十六进制以及十六进制优势