PHP验证码在验证页面中的应用分析
2013-09-04 21:48
309 查看
对于一个网站来说,肯定会需要一个提供验证功能的页面。那么我们现在就需要考虑验证页面中的验证码的应用了。我们接下来将要为大家具体讲解有关一、准备一个展示并提交PHP验证码的页面
<?php @header("content-type:text/html; charset=UTF-8"); //打开session session_start(); ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>PHP验证码示例</title> </head> <body> 验证码:<br/> <iframe id="iimg" height="100" width=300 src="img.php" frameborder="0" ></iframe> <br/> <input type=button value="看不清,换一张" onclick="iimg.location.reload();"> <br> <form action="validate.php" method="post"> 输入验证码:<input name="imgId" style="width:60"> <input type="submit" value="确定"> </form> </body> </html> 二、以下是PHP验证码生成页面,该页面在第一页面中被<img>调用
<?php Header("Content-type: image/gif"); session_start(); //验证码为随机字符,以下是算法 $randval; for($i=0;$i<7;$i++){ $randstr = mt_rand(ord('A'),ord('Z')); srand((double)microtime()*1000000); $randv = mt_rand(0,10); if($randv%2==0){ $randval.=mt_rand(0,10); }else{ $randval.=chr($randstr); } } //注册PHP验证码到session session_register($randval); //以下是绘制验证码图 $height = 50;//图高 $width = 100;//图宽 $im = ImageCreateTrueColor($width, $height); $white = ImageColorAllocate($im, 255, 255, 255); $blue = ImageColorAllocate($im, 0, 0, 64); ImageFill($im, 0, 0, $white); srand((double)microtime()*1000000000); ImageLine($im, mt_rand(0,$width/3), mt_rand(0,$height/3), mt_rand($width/3,$width), mt_rand($height/3,$height), $blue); srand((double)microtime()*1000000); ImageLine($im, mt_rand($width/3,$width), mt_rand(0,$height/3), mt_rand(0,$width/3), mt_rand(0,$height/3), $blue); srand((double)microtime()*1000000); ImageString($im,16 , mt_rand(0,$width - strlen($randval) * 10), mt_rand(0,$height-12), $randval, $blue); ImageGIF($im); ImageDestroy($im); /* 需要注意的是:为了支持以上绘图函数,我们必须在PHP载入GD2图形处理库,可修改 php.ini 文件中的 ;extension=php_gd2.dll 为 extension=php_gd2.dll 即开启GD2库 */ ?> 三、这是验证页面,提示PHP验证码是否通过验证
<?php @header("content-type:text/html; charset=UTF-8"); //开启session session_start(); //得到用户输入的验证码,并转换成大写 $imgId_req = $_REQUEST['imgId']; $imgId_req = strtoupper($imgId_req); //验证该字符串是否注册了session变量 if (session_is_registered($imgId_req)) { echo "<font color=blue >通过验证!</font>"; } else { echo "<font color=red >验证错误!</font>"; } //关闭session,以清除所有注册过的变量 session_destroy(); ?>
<?php @header("content-type:text/html; charset=UTF-8"); //打开session session_start(); ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>PHP验证码示例</title> </head> <body> 验证码:<br/> <iframe id="iimg" height="100" width=300 src="img.php" frameborder="0" ></iframe> <br/> <input type=button value="看不清,换一张" onclick="iimg.location.reload();"> <br> <form action="validate.php" method="post"> 输入验证码:<input name="imgId" style="width:60"> <input type="submit" value="确定"> </form> </body> </html> 二、以下是PHP验证码生成页面,该页面在第一页面中被<img>调用
<?php Header("Content-type: image/gif"); session_start(); //验证码为随机字符,以下是算法 $randval; for($i=0;$i<7;$i++){ $randstr = mt_rand(ord('A'),ord('Z')); srand((double)microtime()*1000000); $randv = mt_rand(0,10); if($randv%2==0){ $randval.=mt_rand(0,10); }else{ $randval.=chr($randstr); } } //注册PHP验证码到session session_register($randval); //以下是绘制验证码图 $height = 50;//图高 $width = 100;//图宽 $im = ImageCreateTrueColor($width, $height); $white = ImageColorAllocate($im, 255, 255, 255); $blue = ImageColorAllocate($im, 0, 0, 64); ImageFill($im, 0, 0, $white); srand((double)microtime()*1000000000); ImageLine($im, mt_rand(0,$width/3), mt_rand(0,$height/3), mt_rand($width/3,$width), mt_rand($height/3,$height), $blue); srand((double)microtime()*1000000); ImageLine($im, mt_rand($width/3,$width), mt_rand(0,$height/3), mt_rand(0,$width/3), mt_rand(0,$height/3), $blue); srand((double)microtime()*1000000); ImageString($im,16 , mt_rand(0,$width - strlen($randval) * 10), mt_rand(0,$height-12), $randval, $blue); ImageGIF($im); ImageDestroy($im); /* 需要注意的是:为了支持以上绘图函数,我们必须在PHP载入GD2图形处理库,可修改 php.ini 文件中的 ;extension=php_gd2.dll 为 extension=php_gd2.dll 即开启GD2库 */ ?> 三、这是验证页面,提示PHP验证码是否通过验证
<?php @header("content-type:text/html; charset=UTF-8"); //开启session session_start(); //得到用户输入的验证码,并转换成大写 $imgId_req = $_REQUEST['imgId']; $imgId_req = strtoupper($imgId_req); //验证该字符串是否注册了session变量 if (session_is_registered($imgId_req)) { echo "<font color=blue >通过验证!</font>"; } else { echo "<font color=red >验证错误!</font>"; } //关闭session,以清除所有注册过的变量 session_destroy(); ?>
相关文章推荐
- php设计模式之单例、多例设计模式的应用分析
- php设计模式之单例、多例设计模式的应用分析
- PHP图形图像的典型应用 --常用图像的应用(验证码)
- JSP页面的验证码生成,验证
- php验证码实现代码(3种)验证类
- PHP中 验证码无法在调用页面正常显示
- 为 PHP 应用提速、提速、再提速!,第 2 部分: 分析 PHP 应用程序以查找、诊断和加速运行缓慢的代码
- 注册页面之前先验证用户名是否存在的php代码
- PHP文件缓存smarty模板应用实例分析
- PHP实现对短信验证码发送次数的限制(防机刷验证码)
- 网络应用:生成随机验证图片并在输入验证码后判断验证码是否正确
- 基于spring mvc的图片验证码,分后台代码和前端页面的展现以及验证码的验证。
- Ajax提交表单时验证码自动验证 php后端验证码检测
- Launcher3源码分析 — 所有应用页面的数据加载和绑定
- awk 分析web日志(页面执行时间)(常见应用3)
- 基于php socket(fsockopen)的应用实例分析
- PHP 验证码生成验证源码
- 验证码与页面验证
- [转]为 PHP 应用提速、提速、再提速!,第 2 部分: 分析 PHP 应用程序以查找、诊断和加速运行缓慢的代码
- Ajax提交表单时验证码自动验证 php后端验证码检测