教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php利用验证码防止恶意注册学习笔记

php利用验证码防止恶意注册学习笔记

发布时间:2016-10-19   编辑:jiaochengji.com
教程集为您提供php利用验证码防止恶意注册学习笔记等资源,欢迎您收藏本站,我们将为您提供最新的php利用验证码防止恶意注册学习笔记资源
常用的防止恶意注册就是利用验证码来实现了,在用户提交注册信息时我随机生成一个图形验证码,这样只有人能识别了,当然简单的验证码机器是机以识别的,所以复杂点的好。


今天我们来研究下PHP验证码,我们通过简单的数字验证码来实现,首先来写一个生成验证码的代码:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7253')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7253>

<?php

//随机生成一个4位数的数字验证码

$num=”";     for($i=0;$i<4;$i ){     $num .= rand(0,9);     }

//4位验证码也可以用rand(1000,9999)直接生成

//将生成的验证码写入session,备验证页面使用

Session_start();     $_SESSION["Checknum"] = $num;

//创建图片,定义颜色值     Header(“Content-type: image/PNG”);

srand((double)microtime()*1000000);

$im = imagecreate(60,20);

$black = ImageColorAllocate($im, 0,0,0);

$gray = ImageColorAllocate($im, 200,200,200);

imagefill($im,0,0,$gray);

//随机绘制两条虚线,起干扰作用

$style = array($black, $black, $black, $black, $black, $gray, $gray, $gray, $gray, $gray);

imagesetstyle($im, $style);

$y1=rand(0,20);     $y2=rand(0,20);     $y3=rand(0,20);     $y4=rand(0,20);

imageline($im, 0, $y1, 60, $y3, IMG_COLOR_STYLED);

imageline($im, 0, $y2, 60, $y4, IMG_COLOR_STYLED)

//在画布上随机生成大量黑点,起干扰作用;

for($i=0;$i<80;$i )     {

imagesetpixel($im, rand(0,60), rand(0,20), $black);     }

//将四个数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成

$strx=rand(3,8);

for($i=0;$i<4;$i ){

$strpos=rand(1,6);     imagestring($im,5,$strx,$strpos, substr($num,$i,1), $black);     $strx =rand(8,12);

}

ImagePNG($im);     ImageDestroy($im);

?>

在reg.php页面我们写一个表单:(此处省去了其他的HTML代码)

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7669')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7669>

<tr>

<td>验证码 :</td>

<td><input type=”text” name=”yzm”style=”width:60px;height:20px;” /><img src=”code.php” onclick=”javascript:this.src=’code.php?’ Math.random();”></img></td>

</tr>

<tr>    <td colspan=’2′><input type=”submit” value=”注册”/></td>

<td>验证码 :</td>

</tr>

因为我们是用post提交的,所以我们用$_POST来获取(在接受页面做验证码的验证:post.php页面)

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4619')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4619>

Session_start();

//back_alert()验证码输入错误的时候,弹出错误信息

function back_alert($yzm){

echo “<script type=’text/javascript’>alert(‘$yzm’);history.back();</script>”;

}

//禁止恶意调用(禁止直接在浏览器打开post.php页面)

if($_POST["yzm"]==null){

back_alert(‘你都木有输入验证码,有木有???’);  }

// 禁止恶意注册

if(!($_POST["yzm"]==$_SESSION["Checknum"])){

back_alert(‘验证码不正确’);

} echo $_POST["yzm"];

您可能感兴趣的文章:
php利用验证码防止恶意注册学习笔记
php验证码大全(实例分享)
php防止恶意刷新日期怎么改
php如何防止恶意刷新访问次数
PHP怎么防止账号批量注册
用PHP实现随机验证码功能
php各种验证码与Ajax验证的实例分享
用PHP实现验证码功能
Python3爬虫进阶:识别极验滑动验证码
帝国cms注册加入问答验证(防垃圾注册机)的代码

[关闭]
~ ~