jtt416

jtt416

这家伙有点懒,还没写个性签名!

  • 财富值10
  • 威望值0
  • 总积分10

个人信息

  • 我也发个吧
    在PHP中

    public function actions()
    	{
    		return array(
    			 'captcha' => array(
    				  'class' => 'yii\captcha\CaptchaAction',
    				  'maxLength' => 5,
    				  'minLength' => 5,
    				  'width'=>120,
    				  'height'=>'27',
    			 ),
    		 );
    	 }
    

    这个大家都知道,在页面中我没用YII的form,而是自己写的表单

    <input type="text" id="codeid" name="checkcode" class="form-control" placeholder="验证码">
                        <div class="input-group-addon" id="logincode">
                        	
                          <img src="/res/images/logincode.gif" id="code_img" alt="点击刷新">
                        </div>
    

    且看JS部分

    <script>
    $(function(){
    	$("#codeid").focus(function(){
    		$.get('<?php echo Yii::$app->urlManager->createUrl('site/captcha');?>?',{refresh:1},function(data){
    			evaldata=eval(data);
    			$("#code_img").attr('src',evaldata.url);	
    		});	
    	});
    	$("#code_img").click(function(){
    		$.get('<?php echo Yii::$app->urlManager->createUrl('site/captcha');?>',{refresh:1},function(data){
    			evaldata=eval(data);
    			$("#code_img").attr('src',evaldata.url);	
    		});
    	});
    })
    </script>
    

    我们通过向/site/captcha?refresh=1发送GET请求,得到的数据如下,

    {"hash1":538,"hash2":538,"url":"/backend/web/site/captcha?v=56a8243edb9a8"}
    

    很清楚的我们得到了一个新的验证码URL,
    这里说下V,V是用PHP的uniqid()生成的一个唯一码
    这个方法不用改YII的任何代码哦

    如果你在model里面进行两次验证码的验证那么你的验证就会通不过哦。

    YII的验证码不论是生成的时候还是验证的时候都是通过了getVerifyCode,验证通过后会重新在生成一次验证码,所以在model里如果你调用了两次验证的话,就通不过哦

  • 支持大家解决问题

实习生 等级规则
10/20
资料完整度
0/100
用户活跃度
0/100

Ta的关注

0

Ta的粉丝

0

Ta的访客

1