本文以实例演示5种验证码,并介绍生成验证码的函数。PHP生成验证码的原理:通过GD库,生成一张带验证码的图片,并将验证码保存在Session中。
页面底部有演示、免费下载链接。若是想看更多js特效、网站源码、 js教程请访问【素材火】 http://www.sucaihuo.com/js 还有演示DEMO,最主要是可以免费下载。1、<br>
HTML<br>
5中验证码HTML代码如下:<br>
<br>
<div> <br>
<h3>1、数字验证码</h3> <br>
<p>验证码:<input> <img alt="图片验证码(用php生成)" ></p> <br>
<p><input></p> <br>
</div> <br>
<div> <br>
<h3>2、数字+字母验证码</h3> <br>
<p>验证码:<input> <img alt="图片验证码(用php生成)" ></p> <br>
<p><input></p> <br>
</div> <br>
<div> <br>
<h3>3、中文验证码</h3> <br>
<p>验证码:<input> <img alt="图片验证码(用php生成)" ></p> <br>
<p><input></p> <br>
</div> <br>
<div> <br>
<h3>4、仿google验证码</h3> <br>
<p>验证码:<input> <img alt="图片验证码(用php生成)" ></p> <br>
<p><input></p> <br>
</div> <br>
<div> <br>
<h3>5、算术验证码</h3> <br>
<p>验证码:<input> <img alt="图片验证码(用php生成)" ></p> <br>
<p><input></p> <br>
</div>
<br>
<br>
2、<br>
js验证<br>
<br>
$(function() { <br>
$("#getcode_num").click(function() { //数字验证 <br>
$(this).attr("src", 'code_num.php?' + Math.random()); <br>
}); <br>
$("#chk_num").click(function() { <br>
var code_num = $("#code_num").val(); <br>
$.post("chk_code.php?act=num", { <br>
code: code_num <br>
}, <br>
function(msg) { <br>
if (msg == 1) { <br>
alert("验证码正确!"); <br>
} else { <br>
alert("验证码错误!"); <br>
} <br>
}); <br>
}); <br>
//数字+字母验证 <br>
$("#getcode_char").click(function() { <br>
$(this).attr("src", 'code_char.php?' + Math.random()); <br>
}); <br>
$("#chk_char").click(function() { <br>
var code_char = $("#code_char").val(); <br>
$.post("chk_code.php?act=char", { <br>
code: code_char <br>
}, <br>
function(msg) { <br>
if (msg == 1) { <br>
alert("验证码正确!"); <br>
} else { <br>
alert("验证码错误!"); <br>
} <br>
}); <br>
}); <br>
//中文验证码 <br>
$("#getcode_zh").click(function() { <br>
$(this).attr("src", 'code_zh.php?' + Math.random()); <br>
}); <br>
$("#chk_zh").click(function() { <br>
var code_zh = escape($("#code_zh").val()); <br>
$.post("chk_code.php?act=zh", { <br>
code: code_zh <br>
}, <br>
function(msg) { <br>
if (msg == 1) { <br>
alert("验证码正确!"); <br>
} else { <br>
alert("验证码错误!"); <br>
} <br>
}); <br>
}); <br>
//google验证 <br>
$("#getcode_gg").click(function() { <br>
$(this).attr("src", 'code_gg.php?' + Math.random()); <br>
}); <br>
$("#chk_gg").click(function() { <br>
var code_gg = $("#code_gg").val(); <br>
$.post("chk_code.php?act=gg", { <br>
code: code_gg <br>
}, <br>
function(msg) { <br>
if (msg == 1) { <br>
alert("验证码正确!"); <br>
} else { <br>
alert("验证码错误!"); <br>
} <br>
}); <br>
}); <br>
//算术验证 <br>
$("#getcode_math").click(function() { <br>
$(this).attr("src", 'code_math.php?' + Math.random()); <br>
}); <br>
$("#chk_math").click(function() { <br>
var code_math = $("#code_math").val(); <br>
$.post("chk_code.php?act=math", { <br>
code: code_math <br>
}, <br>
function(msg) { <br>
if (msg == 1) { <br>
alert("验证码正确!"); <br>
} else { <br>
alert("验证码错误!"); <br>
} <br>
}); <br>
}); <br>
});<br>
<br>
3、<br>
PHP生成验证码<br>
<br>
session_start(); <br>
getCode(4,60,20); <br>
<br>
function getCode($num,$w,$h) { <br>
$code = ""; <br>
for ($i = 0; $i
$code .= rand(0, 9); <br>
} <br>
//4位验证码也可以用rand(1000,9999)直接生成 <br>
//将生成的验证码写入session,备验证时用 <br>
$_SESSION["helloweba_num"] = $code; <br>
//创建图片,定义颜色值 <br>
header("Content-type: image/PNG"); <br>
$im = imagecreate($w, $h); <br>
$black = imagecolorallocate($im, 0, 0, 0); <br>
$gray = imagecolorallocate($im, 200, 200, 200); <br>
$bgcolor = imagecolorallocate($im, 255, 255, 255); <br>
//填充背景 <br>
imagefill($im, 0, 0, $gray); <br>
<br>
//画边框 <br>
imagerectangle($im, 0, 0, $w-1, $h-1, $black); <br>
<br>
//随机绘制两条虚线,起干扰作用 <br>
$style = array ($black,$black,$black,$black,$black, <br>
$gray,$gray,$gray,$gray,$gray <br>
); <br>
imagesetstyle($im, $style); <br>
$y1 = rand(0, $h); <br>
$y2 = rand(0, $h); <br>
$y3 = rand(0, $h); <br>
$y4 = rand(0, $h); <br>
imageline($im, 0, $y1, $w, $y3, IMG_COLOR_STYLED); <br>
imageline($im, 0, $y2, $w, $y4, IMG_COLOR_STYLED); <br>
<br>
//在画布上随机生成大量黑点,起干扰作用; <br>
for ($i = 0; $i
imagesetpixel($im, rand(0, $w), rand(0, $h), $black); <br>
} <br>
//将数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成 <br>
$strx = rand(3, 8); <br>
for ($i = 0; $i
$strpos = rand(1, 6); <br>
imagestring($im, 5, $strx, $strpos, substr($code, $i, 1), $black); <br>
$strx += rand(8, 12); <br>
} <br>
imagepng($im);//输出图片 <br>
imagedestroy($im);//释放图片所占内存 <br>
}
查看该特效演示及免费下载,请访问【素材火】:http://www.sucaihuo.com/js/91.html
AD:真正免费,域名+虚机+企业邮箱=0元