Laravel验证码

Laravel图片验证码步骤:git

1.从github中下载 gregwar/captchagithub

2.用composer集成到Laravel项目中,集成命令为:在composer.json中“require”中添加,“gregwar/captcha”:"1.*"   在和"require"同一级下面添加"minimum-stability":"dev",json

3.经过在命令行中定位到当前文件的目录中输入 composer update 更新session

4.在控制器中引入文件 use Gregwar/Captcha\CaptchaBuilder;composer

函数 dom

//生成验证码
    public  function  verification(){

//        $input = Request::all();
        //生成验证码图片的Builder对象,配置相应属性
        $builder = new CaptchaBuilder;
        //能够设置图片宽高及字体
        $builder->build($width = 100, $height = 40, $font = "0");
        //获取验证码的内容
        $phrase = $builder->getPhrase();

        //把内容存入session
//        Session::flash('milkcaptcha', $phrase);
        //生成图片
        $builder->output();
    }
}

在路由中定义 函数

//验证码
Route::get('verification','LoginController@verification');

在HTML中使用验证码图片:字体

<img src="/verification" onclick="getImage(this)" class = "img" style = "margin:0px;">

js代码ui

<script>
        {{--提交的时候 验证全部的输入是否有值--}}
        function isAllInput(){
            var name = document.getElementById('telephone').value;
            if(name.length<1) document.getElementById("telephone_title").innerHTML="请输入手机号";
            else document.getElementById("telephone_title").innerHTML="";

            var password = document.getElementById('password').value;
            if(password.length<1) document.getElementById("password_title").innerHTML="请输入密码";
            else document.getElementById("password_title").innerHTML="";

            var verification = document.getElementById('verification').value;
            if(verification.length<1) document.getElementById("verification_title").innerHTML="请输入图片验证码";
            else document.getElementById("verification_title").innerHTML="";
        }
//      每个输入的判断 title:提示语  id:当前控件
        function isInput(id,title){
            var inputText = id.value;
            var inputId = id.name;
            if(inputText<1) document.getElementById(inputId+"_title").innerHTML=title;
            else document.getElementById(inputId+"_title").innerHTML="";

        }
        //        获取验证码图片  加上?与Math.random()是为了能获取到图片 不然是不能获取到图片的
        function getImage(id){
            id.src="/verification?"+Math.random();
        }
    </script>
相关文章
相关标签/搜索