分享练习写作代码-php-验证码
最近在使用adodb + smarty 做手机购书网站! 下两个星期就要交了,
可能做不完啦,做多少算多少。 两个星期就做了个登陆和注册! 这速度真慢呀!
很久没写学习笔记了! 下面分享个验证码!和使用
使用页面
<script src="Scripts/jquery-latest.min.js"></script> //jquert的js
<script language="javascript" src="Scripts/Register.js"></script> //验证码的js
<div>
<label>验证码:</label>
<div class="textLine"><input id="yzm" maxlength="4" name="yzm" type="text" style="width:45px;" />
<span id="spYzm"></span>
<input id="ycYzm" name="ycYzm" type="hidden" value="" />
<span id="yzmRrr" class="errShow"></span>
</div>
Register.js // 这里只写验证码关键代码
var jq = $.noConflict(); //将jQuery的$转成jq
jq(document).ready(function(){ //初始化验证码
var iRand = Math.round(Math.random()*100000000); //随机数
var iNmum = iRand.toString().substr(0,4); //截取4位随机数
//给页面添加随机数
document.getElementById("spYzm").innerHTML = '<img id="yzm" src="system/publicClass/captcha.php?yzm='+ iNmum +'" alt="" onclick="Deng.Register.getNewYzm()" />';
//给隐藏域的值赋值随机数 而后验证对比验证码是否正确
document.getElementById("ycYzm").value = iNmum ;
});
if(typeof(Deng) == 'undefined'){ //Deng包 为了避免重名
Deng = function(){}
}
Deng.Register = function(){} //Deng.Register 包
//获取新的验证码
Deng.Register.getNewYzm = function(){
var iRand = Math.round(Math.random()*100000000);
var iNmum = iRand.toString().substr(0,4);
document.getElementById("spYzm").innerHTML = '<img id="yzm" src="system/publicClass/captcha.php?yzm='+ iNmum +'" alt="" onclick="Deng.Register.getNewYzm()" />';
document.getElementById("ycYzm").value = iNmum ;
}
captcha.php //实现验证码页面
<?php
//echo mt_rand(0,255);
$yzm = $_GET["yzm"]; //获取页面传过来的4位数字
header ("Content-type: image/png");
//$pic=imagecreatefrompng("background.png");
$width=60;
$height=40;
$pic = imagecreate($width,$height ); //实例一个图片资源
$byColorr=imagecolorallocate($pic,255,255,255); //设置白色背景
imagefill($pic,0,0,$byColorr); //填充背景颜色
$font = "C:/WINDOWS/Fonts/SIMFANG.TTF"; //字体路径
$str = iconv( "gb2312","UTF-8",$yzm); //将字体转换成utf才能显示中文
//将传过来的4位数字分别设置大小 位置 颜色
imagettftext($pic,mt_rand(12,20), mt_rand(-30,30), 5,mt_rand(20,$height),imagecolorallocate($pic,0,mt_rand(0,255),mt_rand(0,255)),$font,substr($str,0,1));
imagettftext($pic,mt_rand(12,20), mt_rand(-20,30), 15,mt_rand(20,$height),imagecolorallocate($pic,mt_rand(0,255),0,mt_rand(0,255)),$font,substr($str,1,1));imagettftext($pic,mt_rand(12,20), mt_rand(-40,20), 30,mt_rand(20,$height),imagecolorallocate($pic,mt_rand(0,255),mt_rand(0,255),0),$font,substr($str,2,1));imagettftext($pic,mt_rand(12,20), mt_rand(-20,30), 43,mt_rand(20,$height),imagecolorallocate($pic,0,mt_rand(0,255),mt_rand(0,255)),$font,substr($str,3,1));
for($i =0 ; $i<100; $i++){ //添加100个点
imagesetpixel($pic,mt_rand(0,$width),mt_rand(0,$height),imagecolorallocate($pic,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)));
}
imagepng($pic); //生成png
imagedestroy($pic); //清空内存
?>