php生成验证码与验证码验证完整实例

标题文档

<!DOCTYPE
 html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html
 xmlns="
http://www.w3.org/1999/xhtml">

<head>

<meta
 http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>无标题文档</title>

<script
 language="javascript">

function refresh_code()

{

form1.imgcode.src="verifycode.php?a="+Math.random();

}

</script>

</head>

<body>

<form
 id="form1" name="form1" method="post" action="checkcode.php">

<label
 for="code">验证码:</label>

<input
 type="text" name="code" id="textfield" />

<img
 id="imgcode" src="VerifyCode.php" alt="验证码" />

<a
 href="javascript:refresh_code()">看不清?换一个</a>

<input
 type="submit" name="button" id="button" value="提交" />

</form>

</body>

</html>

verifycode.php文件代码如下

<?php

/*

图片验证码 Powered By KASON test <a href="
http://www.hzhuti.com/nokia/c6/">
http://www.hzhuti.com/nokia/c6/</a>   */

session_start();

$num=4;//验证码个数

$width=80;//验证码宽度

$height=20;//验证码高度

$code=‘ ‘;

for($i=0;$i<$num;$i++)//生成验证码

{

switch(rand(0,2))

{

case
 0:$code[$i]=chr(rand(48,57));break;//数字

case
 1:$code[$i]=chr(rand(65,90));break;//大写字母

case
 2:$code[$i]=chr(rand(97,122));break;//小写字母

}

}

$_SESSION["VerifyCode"]=$code;

$image=imagecreate($width,$height);

imagecolorallocate($image,255,255,255);

for($i=0;$i<80;$i++)//生成干扰像素

{

$dis_color=imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255));

imagesetpixel($image,rand(1,$width),rand(1,$height),$dis_color);

}

for($i=0;$i<$num;$i++)//打印字符到图像

{

$char_color=imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255));

imagechar($image,60,($width/$num)*$i,rand(0,5),$code[$i],$char_color);

}

header("Content-type:image/png");

imagepng($image);//输出图像到浏览器

imagedestroy($image);//释放资源

?>

checkcode.php文件如下

<meta http-equiv="Content-Type"
 content="text/html; charset=utf-8"
 />

<?php

ini_set(‘display_errors‘,‘Off‘);

session_start();

if((strtoupper($_POST["code"])) ==strtoupper(($_SESSION["VerifyCode"]))){

print("验证码正确,");

}else{

print("验证码错误,");

}

echo
 "提交的验证码:".strtoupper($_POST["code"]).",正确的验证码:".strtoupper($_SESSION["VerifyCode"]);

?>

时间: 2024-08-24 21:43:20

php生成验证码与验证码验证完整实例的相关文章

第二百七十节,Tornado框架-生成验证码图片,以及验证码结合Session验证

Tornado框架-生成验证码图片,以及验证码结合Session验证 第一.生成验证码图片  生成验证码图片需要两个必须模块 1.python自带的random(随机模块) 2.Pillow()图像处理模块里的PIL(图像库),为第三方模块,需要安装 封装验证码图片生成插件py 在封装文件里先导入random(随机模块),和Pillow()图像处理模块里的所需py文件 封装验证码图片生成插件功能,调用后返回验证码图片,和字符串类型验证码,两个返回值 注意:验证码需要一个字体文件,这个字体文件必须

PHP利用jquery生成各种验证码和Ajax验证

PHP生成验证码图片 PHP生成验证码的原理:使用PHP的GD库,生成一张带验证码的图片,并将验证码保存在Session中.PHP 生成验证码的大致流程有: 1.产生一张png的图片: 2.为图片设置背景色: 3.设置字体颜色和样式: 4.产生4位数的随机的验证码: 5.把产生的每个字符调整旋转角度和位置画到png图片上: 6.加入噪点和干扰线防止注册机器分析原图片来恶意破解验证码: 7.输出图片: 8.释放图片所占内存. 下面我们讲解验证码的生成过程,直接上代码. session_start(

PHP生成各种验证码和Ajax验证

网址:http://www.helloweba.com/view-blog-191.html 源码下载地址: http://files.cnblogs.com/h07061108/php%E9%AA%8C%E8%AF%81%E7%A0%81.rar PHP生成各种验证码和Ajax验证

微信小程序发送短信验证码完整实例

微信小程序注册完整实例,发送短信验证码,带60秒倒计时功能,无需服务器端.效果图: 代码: index.wxml <!--index.wxml--> <view class="container"> <view class='row'> <input placeholder='请输入姓名' bindinput='bindNameInput'/> </view> <view class='row'> <inpu

【代码实现】PHP生成各种随机验证码

文章来源:PHP开发学习门户  (自行开发的个人网站) 验证码在WEB应用中非常重要,通常用来防止用户恶意提交表单,如恶意注册和登录.论坛恶意灌水等.本文将通过实例讲解使用PHP生成各种常见的验证码包括数字验证码.数字+字母验证码.中文验证码.算术验证码等等以及其Ajax验证过程. 下载示例源码 PHP生成验证码图片 PHP生成验证码的原理:使用PHP的GD库,生成一张带验证码的图片,并将验证码保存在Session中.PHP生成验证码的大致流程有: 1.产生一张png的图片: 2.为图片设置背景

django图片验证码和滑动验证

1. django-simple-captcha 模块 安装 django-simple-captcha pip install django-simple-captcha pip install Pillow 注册 和注册 app 一样,captcha 也需要注册到 settings 中.同时它也会创建自己的数据表,因此还需要数据同步. # settings.py INSTALLED_APPS = [ ... 'captcha', ] # 执行命令进行数据迁徙,会发现数据库中多了一个 capt

java实现生成简单图片验证码

原文:java实现生成简单图片验证码 源代码下载地址:http://www.zuidaima.com/share/1550463428840448.htm 项目载图: 该项目要转换为Dynamic web project http://www.zuidaima.com/blog/1618162161323008.htm 另外缺少jar包: http://www.zuidaima.com/jar/search/jstl-1.0.1.htm http://www.zuidaima.com/jar/s

python随机生成6位数验证码

#随机生成6位数验证码 import randomcode = []for i in range(6):    if i == str(random.randint(1,5)):        code.append(i)    else:       temp =  random.randint(65,90)       code.append(chr(temp)) print ''.join(code) ###扩充random用法,随机生成树,和程序无关 print random.rando

js随机生成4位验证码

方法一: /*随机生成4位验证码*/ /*step1:将所有字母,数字装入一个数组备用*/ var codes=[]; //数字:48-57;unicode编码 for(var i=48;i<57;codes.push(i),i++); /*console.log(codes);*/ //大写字母:65-90;unicode编码 for(var i=60;i<90;codes.push(i),i++); //小写字母:97-122;unicode编码 for(var i=97;i<122