2、载入验证码类及$_SESSION处理

1、载入验证码类,并验证

(1)下载定义好的code验证码类,放置到resources目录下

 

(2) 添加路由

Route::get('/admin/code','Admin\LoginController@code');

(3)添加方法 LoginController.php

public function code()
    {
         $code = new \Code();
         $code->make();
    }

注意: 不要忘记引入Code类,不加‘\’,会引入App\Http\Controllers\Admin\Code

require_once 'resources/code/Code.class.php';

验证:

 

2、$_SESSION处理 (引入第三方类时)

查看Code类,可以看到其中的get方法,验证session

public function get() {
        return $_SESSION['code'];
    }

    //生成验证码
private function createCode() {
        $code = '';
        for ($i = 0; $i < $this->codeLen; $i++) {
            $code .= $this->codeStr [mt_rand(0, strlen($this->codeStr) - 1)];
        }
        $this->code = strtoupper($code);
        $_SESSION['code'] = $this->code;
    }

(1)添加测试路由

Route::get('/admin/getcode','Admin\LoginController@getcode');

(2)添加方法 LoginController.php

public function getcode()
    {
         $code = new \Code();
         echo $code->get();
    }

验证:

原因: laravel对session进行封装,未开启原始session

修改:

刚刚将server.php重命名为index.php,修改该文件,打开session_start();

 1 <?php
 2 
 3 /**
 4  * Laravel - A PHP Framework For Web Artisans
 5  *
 6  * @package  Laravel
 7  * @author   Taylor Otwell <taylorotwell@gmail.com>
 8  */
 9 
10 session_start();
11 
12 $uri = urldecode(
13     parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
14 );
15 
16 // This file allows us to emulate Apache's "mod_rewrite" functionality from the
17 // built-in PHP web server. This provides a convenient way to test a Laravel
18 // application without having installed a "real" web server software here.
19 if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
20     return false;
21 }
22 
23 require_once __DIR__.'/public/index.php';

刷新

3、将验证码引入到登陆页面

(1)修改login.blade.php

1 <input type="text" class="code" name="code"/>
2 <span><i class="fa fa-check-square-o"></i></span>
3 <img src="{{url('admin/code')}}" alt="">

(2)验证

http://127.0.0.1/admin/login(此时显示验证码,而点击验证码,并不能更新)

 (3)验证码刷新功能

 添加onclick 事件

<img src="{{url('admin/code')}}" alt="" onclick="this.src='{{url('admin/code')}}'">

 

如图可以看到:访问地址一样,有的浏览器误以为是一样的地址,不会再去请求,所以加一个随机数区分

<img src="{{url('admin/code')}}" alt="" onclick="this.src='{{url('admin/code')}}?'+Math.random()">

 

 

 

posted @ 2017-01-23 17:28  封情葬爱  阅读(693)  评论(0编辑  收藏  举报