解決laravel session失效的問題
阿新 • • 發佈:2020-11-19
最新在學習laravel,用到了session,因為laravel沒法用$_SESSION 所以只能用框架的session。
貼上程式碼
<?php namespace App\Http\Controllers; use App\Http\Requests; use Request; use Illuminate\Support\Facades\Session; class CommonController extends Controller { static function login(){ $team_id=Request::input('team_id'); $uuid=Request::input('uuid'); $key=Request::input('key'); if(empty($team_id)){ $team_id=Session::get('team_id'); } if(empty($uuid)){ $uuid=Session::get('uuid'); } if(empty($key)){ $key=Session::get('key'); } // session(['team_id'=>$team_id]); Session::put('team_id',$team_id); Session::put('uuid',$uuid); Session::put('key',$key); Session::save(); } public static function islogin(){ $team_id=Session::get('team_id'); $uuid=Session::get('uuid'); $key=Session::get('key'); if(!empty($team_id)&&!empty($uuid)){ if($key != 1234){ echo "沒有許可權"; exit; } }else{ echo "沒有許可權"; exit; } } }
在當前頁面可以到SESSION,但是跨頁面就失效,以為是AJAX的CSRF驗證問題,查詢試了不是,然後經過打印發現2個SESSION不一致,繼續檢查最後發現是在定義路由的時候沒有定義在同一個分組內所以導致SESSION不一致。
將路由重新定義好了
Route::group(['middleware'=>'web'],function() { Route::any('/report/billviews', 'report\UserbillController@BillViews'); Route::any('/report/index','report\UseraccessController@index');//把需要用到session的路由請求全部放在web組裡。 Route::any('/report/countprice', 'report\UserbillController@CountPrice'); Route::any('islogin', 'CommonController@islogin'); Route::any('login', 'CommonController@login'); });
還有個坑laravel5.2的session必須要過中介軟體
以上這篇解決laravel session失效的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援碼農教程。