PHP实现字母数字混合验证码

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

验证码调用地址:Application\Home\Controller\CodeController.class.php

1 Vendor('Vcode.Vcode', '', '.class.php'); 
2 $config = array("width" => 100, "height" => 36, "count" => 4, "str" => 2); //配置 
3 $vcode = new \Vcode($config); 
4 $vcode->getCode(); //获取验证码 
5 $vcode->getImg(); //输出图片 
6 exit;

 

验证码图片

1 <img src="__APP__/code/" id="code" οnclick="changeCode($('#code'))"/>

 

JS通过后缀加随机数Math.random()来刷新验证码

1 function changeCode(obj) { 
2    obj.attr("src", '__APP__/code/?' + Math.random()); 
3 }

 

检测验证码是否输入正确

 1 <input type="text" id="input_code" class="input"/> 
 2 <input type="button" value="提交" class="btn" οnclick="checkCode()"/>
 3 function checkCode() { 
 4     $.post("__APP__/Code/check", {code: $("#input_code").val()}, function(data) { 
 5         if (data == '1') { 
 6             alert("验证码正确!"); 
 7         } else { 
 8             alert("验证码错误!"); 
 9         } 
10     }, "json") 
11 }

 

PHP验证传过来的参数code和当前session存储的验证码进行比较,若是正确返回1,错误则-1

1 public function check() { 
2         $code = I('post.code'); 
3         if (strtolower($code) == $_SESSION["sucaihuo_code"]) { 
4             echo "1"; 
5         } else { 
6             echo "-1"; 
7         } 
8 }

 

本文转自:https://www.sucaihuo.com/php/533.html 转载请注明出处!

posted on 2019-07-11 11:11 我乐意啊 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/woleyia/p/11168786.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值