一款简单的PHP实现字母数字混合验证码,支持自定义验证码、验证码图片、宽度、高度、个数、背景图片等

验证码调用地址:Application\Home\Controller\CodeController.class.php
Vendor('Vcode.Vcode', '', '.class.php');
$config = array("width" => 100, "height" => 36, "count" => 4, "str" => 2); //配置
$vcode = new \Vcode($config);
$vcode->getCode(); //获取验证码
$vcode->getImg(); //输出图片
exit;
验证码图片
<img src="https://www.freexyz.cn/dev/__APP__/code/" id="code" onclick="changeCode($('#code'))"/>
JS通过后缀加随机数Math.random()来刷新验证码
function changeCode(obj) {
obj.attr("src", '__APP__/code/?' + Math.random());
}
检测验证码是否输入正确
<input type="text" id="input_code" class="input"/>
<input type="button" value="提交" class="btn" onclick="checkCode()"/>
function checkCode() {
$.post("__APP__/Code/check", {code: $("#input_code").val()}, function(data) {
if (data == '1') {
alert("验证码正确!");
} else {
alert("验证码错误!");
}
}, "json")
}
PHP验证传过来的参数code和当前session存储的验证码进行比较,若是正确返回1,错误则-1
public function check() {
$code = I('post.code');
if (strtolower($code) == $_SESSION["sucaihuo_code"]) {
echo "1";
} else {
echo "-1";
}
}
总结
以上所述是小编给大家介绍的PHP实现字母数字混合验证码功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对免费资源网网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

评论(0)