Laravel如何在会话超时锁定屏幕后实现

时间:2016-12-07 06:22:43

标签: php session laravel-5

我尝试了多种来自互联网的方式,但是他们无法实现,我希望有人可以给我一种实现想法或方法的方法,谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

假设您正在使用会话驱动程序来处理身份验证,您可以在

中更改空闲会话的到期时间。

/app/config/session.php文件。

/*
|--------------------------------------------------------------------------
| Session Lifetime
|--------------------------------------------------------------------------
|
| Here you may specify the number of minutes that you wish the session
| to be allowed to remain idle before it expires. If you want them
| to immediately expire on the browser closing, set that option.
|
*/

'lifetime' => 120,    // minutes

'expire_on_close' => false,

答案 1 :(得分:0)

让我举个例子。在SessionTimeout文件夹中定义app\Http\Middleware中间件。

<?php

namespace App\Http\Middleware;

use Closure;
use Auth;
use Session;

class SessionTimeout
{


     /**
     * Check the incoming request for session data, log out if session lifetime is exceeded.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */

     public function handle($request, Closure $next)
     {

        //$isLoggedIn = $request->path() != '/logout';

        $bag = Session::getMetadataBag();

        $max = $this->getTimeOut();

        if (($bag && $max < (time() - $bag->getLastUsed()))) {

            //$cookie = cookie('intend', $isLoggedIn ? url()->current() : 'auth/login');

            $email = Auth::user()->email;

            $returnPath = url()->current();

            $request->session()->flush(); // remove all the session data

            Auth::logout(); // logout user

            return redirect('auth/login')
                    ->withInput(compact('email', 'returnPath'))
                    //->withCookie($cookie)
                    ->withErrors(['Please login']);
            //you could also redirect to lock-screen, a completely different view 
            //and then pass the returnPath to controller method maybe via hidden filed
            //to redirect to the last page/path the user was on 
            //after successful re-login from the lock-screen.
        }

        return $next($request);


     }

     /**
     * Set a variable in .env file TIMEOUT (in seconds) to play around in the development machine.
     */
     protected function getTimeOut()
     {
        return (env('TIMEOUT')) ?: (config('session.lifetime') * 60);
     }
}  

SessionTimeout添加到app\Http\Kernel.php

class Kernel extends HttpKernel {
 /**
 * The application's global HTTP middleware stack.
 *
 * @var array
 */
 protected $middleware = [
      'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
      'Illuminate\Cookie\Middleware\EncryptCookies',
      'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
      'Illuminate\Session\Middleware\StartSession',
      'Illuminate\View\Middleware\ShareErrorsFromSession',
      'App\Http\Middleware\SessionTimeout'
 ];
 /**
 * The application's route middleware.
 *
 * @var array
 */
 protected $routeMiddleware = [
      'auth' => 'App\Http\Middleware\Authenticate',
      'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
      'guest' => 'App\Http\Middleware\RedirectIfAuthenticated'
 ];

}

然后在resources\views\auth\login.blade.php

中的登录表单视图中
@extend('app-layout')
@section('content')
    //code to display errors here

    @if($email) //check if the request has $email returned by SessionTimeout middleware
        //if so display lock screen like
        //code to display the profile image
        //code to display the user email (or whatever id is used)
    @else
        //display email input field for a new login
        //code to input the email (whatever id is used) for a new login
    @endif
    //here the code common for lock screen as well as new login.
    //code to display input password 
    //code for submit button and rest of the things like remember me field
@stop  

您还可以使用部分锁定屏幕和新登录表单并根据@if($email)显示。

希望这会让你开始。