php生成图片验证码-附五种验证码

php⽣成图⽚验证码-附五种验证码
以前输出验证码的时候⽤过⼀个⽅法,在前台⽤JS⽣成验证码字符串,再传递到后台⽤PHP输出验证码图像。这样在验证时就不需要使⽤$_SESSION传递验证码
的值,直接⽤JS⽐较⽣成的字符串和输⼊的字符串是否相等即可。
本⽂以实例演⽰5种验证码,并介绍⽣成验证码的函数。PHP⽣成验证码的原理:通过GD库,⽣成⼀张带验证码的图⽚,并将验证码保存在Session中。
诺基亚36601、HTML
5中验证码HTML代码如下:
<div class="demo">
<h3>1、数字验证码</h3>
<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>
</div>
<div class="demo">
<h3>2、数字+字母验证码</h3>
<p>验证码:<input type="text" class="input" id="code_char" maxlength="4" /> <img src="code_char.php" id="getcode_char" title="看不清,点击换⼀张" align="absmiddle" /></p>
<p><input type="button" class="btn" id="chk_char" value="提交" /></p>
</div>雷傲论坛
<div class="demo">
<h3>3、中⽂验证码</h3>
<p>验证码:<input type="text" class="input" id="code_zh" maxlength="4" /> <img src="code_zh.php" id="getcode_zh" title="看不清,点击换⼀张" align="absmiddle" /></p>
<p><input type="button" class="btn" id="chk_zh" value="提交" /></p>
</div>
<div class="demo">
<h3>4、仿google验证码</h3>
<p>验证码:<input type="text" class="input" id="code_gg" maxlength="4" /> <img src="code_gg.php" id="getcode_gg" title="看不清,点击换⼀张" align="absmiddle" /></p>
<p><input type="button" class="btn" id="chk_gg" value="提交" /></p>
</div>
<div class="demo">
<h3>5、算术验证码</h3>
<p>验证码:<input type="text" class="input" id="code_math" maxlength="4" /> <img src="code_math.php" id="getcode_math" title="看不清,点击换⼀张" align="absmiddle" /></p>
<p><input type="button" class="btn" id="chk_math" value="提交" /></p>
</div>
2、js验证
$(function() {
$("#getcode_num").click(function() { //数字验证
$(this).attr("src", 'code_num.php?' + Math.random());
});
$("#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("验证码错误!");
}
});
});
//数字+字母验证
$("#getcode_char").click(function() {
$(this).attr("src", 'code_char.php?' + Math.random());
});
$("#chk_char").click(function() {
var code_char = $("#code_char").val();
$.post("chk_code.php?act=char", {
code: code_char
},
function(msg) {
if (msg == 1) {历史研究投稿
alert("验证码正确!");
} else {
alert("验证码错误!");
}
});
});
//中⽂验证码
$("#getcode_zh").click(function() {
$(this).attr("src", 'code_zh.php?' + Math.random());
});
$("#chk_zh").click(function() {
山东威海大学
var code_zh = escape($("#code_zh").val());
$.post("chk_code.php?act=zh", {
code: code_zh
},
function(msg) {
环境与健康杂志
if (msg == 1) {
alert("验证码正确!");
} else {
alert("验证码错误!");
}
});
});
//google验证
$("#getcode_gg").click(function() {
$(this).attr("src", 'code_gg.php?' + Math.random());
});
$("#chk_gg").click(function() {
var code_gg = $("#code_gg").val();
$.post("chk_code.php?act=gg", {
code: code_gg
},
function(msg) {
if (msg == 1) {
alert("验证码正确!");
} else {
alert("验证码错误!");
}
});
});
//算术验证
$("#getcode_math").click(function() {
$(this).attr("src", 'code_math.php?' + Math.random());
});
$("#chk_math").click(function() {
var code_math = $("#code_math").val();
$.post("chk_code.php?act=math", {
code: code_math
},
function(msg) {
if (msg == 1) {
alert("验证码正确!");
} else {
alert("验证码错误!");
}
});
});
});
3、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);//释放图⽚所占内存
}
以上内容就是php⽣成图⽚验证码-附五种验证码的全部内容,希望⼤家喜欢。

本文发布于:2024-09-24 19:22:28,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/153241.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:验证码   验证   虚线   数字   间距   字符   输出   字符串
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议