我試過了很多從網上下載的方法,但都無法實現,希望有人能給我一種實現想法或方法的方法,謝謝你的幫助。Laravel如何實現會話超時後鎖定屏幕
回答
讓我舉個例子。在app\Http\Middleware
文件夾中定義SessionTimeout
中間件。
<?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);
}
}
的到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
添加SessionTimeout
那麼你也可以使用諧音爲鎖屏以及基於的新登錄表單和顯示。
希望這會讓你開始。
感謝您的回覆,我找到了解決方案 –
@KylinSky請分享您找到的解決方案 – hazelcodes
假設你正在使用的會話驅動程序來處理身份驗證,您可以更改時間段空閒會話在
/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. AppleScript的超時,當屏幕被鎖定
- 2. 在Android 2.0中實現屏幕鎖定
- 3. Android,如何禁用鎖定屏幕/屏幕超時(API版本無關)
- 4. 屏幕鎖定後重置計時器
- 5. 如何鎖定屏幕?
- 6. 會話的Oracle設置鎖定超時
- 7. Php會話鎖定等待超時
- 8. 鎖定屏幕
- 9. 鎖定屏幕
- 10. 如何解鎖鎖定屏幕?
- 11. NetworkError:500會話超時後出現System.ServiceModel.ServiceActivationException
- 12. 喚醒屏幕並在鎖定屏幕上顯示對話框
- 13. 鎖定MFC屏幕
- 14. 屏幕鎖定Contineously
- 15. 麻煩實現自定義鎖定屏幕(後臺活動問題)
- 16. 自定義鎖定屏幕有時被股票鎖定屏幕覆蓋
- 17. 鎖定屏幕後調用Android onCreate
- 18. 在屏幕滾動過多之後,在屏幕上鎖定div?
- 19. 如何在收件箱中點擊對話鎖定屏幕?
- 20. iPhone,如何在鎖定屏幕上顯示更改對話框?
- 21. 如何在Java中實現會話超時?
- 22. 我如何在iPhone中實現會話超時?
- 23. 如何在角度js中實現會話超時?
- 24. 如何實現會話超時頁面使用asp.net mvc的
- 25. 如何在OpenERP中實現會話超時
- 26. iOS 10通知不會出現在鎖定屏幕上
- 27. 如何在Windows中鎖定屏幕?
- 28. 如何遠程鎖定iphone屏幕
- 29. 如何揭示該屏幕被鎖定?
- 30. 如何在會話超時後重定向到指定頁面
你是什麼意思「會話超時鎖屏後」? –
用戶登錄超時,只輸入密碼,而不是第二次登錄,謝謝你的回覆:) –
你的意思是一段時間後自動註銷嗎? –