考证码在WEB应用中非常重要,一般用来防备用户歹意提交表单,如歹意注册和登录、论坛歹意注水等。本文将经由历程实例解说应用PHP生成种种罕见的考证码包含数字考证码、数字+字母考证码、中文考证码、算术考证码等等以及其Ajax考证历程。
PHP生成考证码图片
PHP生成考证码的道理:应用PHP的GD库,生成一张带考证码的图片,并将考证码保存在Session中。PHP生成考证码的大抵流程有:
1、发生一张png的图片;
2、为图片设置背景色;
3、设置字体色彩和款式;
4、发生4位数的随机的考证码;
5、把发生的每一个字符调解扭转角度和位置画到png图片上;
6、到场噪点和滋扰线防备注册机械剖析原图片来歹意破解考证码;
7、输出图片;
8、开释图片所占内存。
相干引荐:《php入门教程》
示例代码:
session_start(); getCode(4,60,20); function getCode($num,$w,$h) { $code = ""; for ($i = 0; $i < $num; $i++) { $code .= rand(0, 9); } //4位考证码也能够用rand(1000,9999)直接生成 //将生成的考证码写入session,备考证时用 $_SESSION["helloweba_num"] = $code; //竖立图片,定义色彩值 header("Content-type: image/PNG"); $im = imagecreate($w, $h); $black = imagecolorallocate($im, 0, 0, 0); $gray = imagecolorallocate($im, 200, 200, 200); $bgcolor = imagecolorallocate($im, 255, 255, 255); //添补背景 imagefill($im, 0, 0, $gray); //画边框 imagerectangle($im, 0, 0, $w-1, $h-1, $black); //随机绘制两条虚线,起滋扰作用 $style = array ($black,$black,$black,$black,$black, $gray,$gray,$gray,$gray,$gray ); imagesetstyle($im, $style); $y1 = rand(0, $h); $y2 = rand(0, $h); $y3 = rand(0, $h); $y4 = rand(0, $h); imageline($im, 0, $y1, $w, $y3, IMG_COLOR_STYLED); imageline($im, 0, $y2, $w, $y4, IMG_COLOR_STYLED); //在画布上随机生成大批斑点,起滋扰作用; for ($i = 0; $i < 80; $i++) { imagesetpixel($im, rand(0, $w), rand(0, $h), $black); } //将数字随机显现在画布上,字符的程度间距和位置都按肯定波动局限随机生成 $strx = rand(3, 8); for ($i = 0; $i < $num; $i++) { $strpos = rand(1, 6); imagestring($im, 5, $strx, $strpos, substr($code, $i, 1), $black); $strx += rand(8, 12); } imagepng($im);//输出图片 imagedestroy($im);//开释图片所占内存 }
代码中自定义函数getCode()解释了考证码的生成历程。应用PHP GD库自带的图象处置惩罚函数,能轻松生成种种想要的图片结果。
·imagecreate():竖立一个新图象
·imagecolorallocate():为图象分派色彩
·imagefill():添补图象
·imagerectangle():画一个矩形(边框)
·imagesetstyle():设置画线作风
·imageline():画一条线段
·imagesetpixel():画点像素
·imagepng():以PNG花样将图象输出到浏览器或文件
·imagedestroy():开释图片所占内存
将上述代码保存为code_num.php,以便挪用。
Ajax革新和考证
考证码生成后,我们要在现实的项目中应用,一般我们应用ajax能够完成点击考证码时革新生成新的考证码(偶然生成的考证码肉眼很难辨认),即“看不清换一张”。填写考证码后,还需要考证所填考证码是不是准确,考证的历程是要背景顺序来完成,然则我们也能够经由历程ajax来完成无革新考证。
我们竖立一个前端页面index.html,载入jQuery,同时在body中到场考证码表单元素:
<p>考证码:<input type="text" class="input" id="code_num" name="code_num" maxlength="4" /> <img src="code_num.php" id="getcode_num" title="看不清,点击换一张" align="absmiddle"></p> <p><input type="button" class="btn" id="chk_num" value="提交" /></p>
html代码中,<img src=”code_num.php”即挪用了生成的考证码,当点击考证码时,革新生成新的考证码:
$(function(){ //数字考证 $("#getcode_num").click(function(){ $(this).attr("src",'code_num.php?' + Math.random()); }); ... });
革新考证码,实在就是从新要求了考证码生成顺序,这里要注意的是挪用code_num.php时要带上随机参数防备缓存。接下来填写好考证码以后,点“提交”按钮,经由历程$.post(),前端向背景chk_code.php发送ajax要求。
$(function(){ ... $("#chk_num").click(function(){ var code_num = $("#code_num").val(); $.post("chk_code.php?act=num",{code:code_num},function(msg){ if(msg==1){ alert("考证码准确!"); }else{ alert("考证码毛病!"); } }); }); });
背景chk_code.php考证:
session_start(); $code = trim($_POST['code']); if($code==$_SESSION["helloweba_num"]){ echo '1'; }
背景依据提交的考证码与保存在session中的考证码比对,完成考证。
关于其他几种考证的生成和应用,其道理一样,开发者能够依据需要,发生多种款式的随机考证码,本文演示demo中供应了数字考证码、数字+字母考证码、中文考证码、仿google考证码,算术考证码等。
以上就是php怎样随机生成考证码的细致内容,更多请关注ki4网别的相干文章!