头像

THINKPHP原创验证码

2018-07-28 10:39:16 收藏    来源:PHP代码   浏览()   评论 ( 0 )   

THINKPHP原创验证码
今天给朋友们分享一个简单的验证码,支持自定义验证码、验证码图片、宽度、高度、个数、背景图片等
下载资源:0

手机扫码访问:

下载资源 下载积分:100

验证码调用地址:

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代码)并保留原文链接:http://www.phpdaima.com/Thinkphp-917.html
广告不存在
评论0

后面还有条评论,点击查看>>

温馨提示:为规范评论内容,垃圾评论一律封号...

THINKPHP原创验证码
我的积分余额: 0.0 已下载次数: 0
所需积分:100100 开始下载
×