温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php如何实现字母数字混合验证码?

发布时间:2020-05-21 17:33:07 来源:亿速云 阅读:241 作者:鸽子 栏目:编程语言

验证码效果如图:

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="__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实现字母数字混合验证码的详细内容,更多请关注亿速云其它相关文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI