重定向页面时 Laravel Auth 会话丢失
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24053235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Auth session losing when redirect pages
提问by KadirCanerErgun
I have a auth problem with laravel sessions. (It was working in same system before)
我有 Laravel 会话的身份验证问题。(它之前在同一系统中工作)
This is my user controller
这是我的用户控制器
public function postLogin()
{
$username = Input::get("username");
$password = Input::get("password");
$credentials = array
(
"username"=> $username,
"password" => $password,
);
if (Auth::attempt($credentials))
{
echo Auth::user()->username;
exit();
}
else
{
return Redirect::to("user/login")->with("error_msg","Giri? Ba?ar?s?z! <br>Lütfen girdi?iniz bilgileri kontrol edip yeniden deneyin.");
}
}
It returns username.. But when i redirect page session is losing.
它返回用户名.. 但是当我重定向页面会话丢失。
public function postLogin()
{
$username = Input::get("username");
$password = Input::get("password");
$credentials = array
(
"username"=> $username,
"password" => $password,
);
if (Auth::attempt($credentials))
{
return Redirect::to('check_login');
}
else
{
return Redirect::to("user/login")->with("error_msg","Giri? Ba?ar?s?z! <br>Lütfen girdi?iniz bilgileri kontrol edip yeniden deneyin.");
}
}
And My Route:
还有我的路线:
Route::get("check_login",function(){
echo Auth::user()->username;
});
The result:
结果:
ErrorException
Trying to get property of non-object
echo Auth::user()->username;
回答by PJ3
use database session, more info: http://laravel.com/docs/5.0/session#database-sessions
使用数据库会话,更多信息:http: //laravel.com/docs/5.0/session#database-sessions
OR
或者
I got the same problem. & found problem with my code
我遇到了同样的问题。发现我的代码有问题
in my controller:
在我的控制器中:
public function login(Request $request){
//assign $request data to session
Session::put('price',$request->price);
}
public function checkLogin(){
if(login fail){
redirect back to login
}
}
when i redirect to login session override to empty because when I redirect, $request is empty so 'price' is empty
当我重定向到登录会话覆盖为空时,因为当我重定向时,$request 为空,因此“价格”为空
so I check session already got value before put into session
所以我在进入会话之前检查会话已经获得了价值
public function login(Request $request){
//check session already got value
if(Session::get('price') == ""){
//assign $request data to session
Session::put('price',$request->price);
}
}
problem solved for me.
问题为我解决了。
hope it helps to anyone have same problem
希望对有同样问题的人有所帮助
回答by Khay
Check your route for postLogin()
function. If you are using in UserController
, it should be below for post
method.
检查您的路线的postLogin()
功能。如果你使用 in UserController
,它应该在post
方法下面。
Route::post('login', array('uses' => 'UserController@postLogin'));
Function and route that you provided is working perfectly with me, but if you want to check the user is saved in session or not, try like this.
您提供的函数和路由与我完美配合,但是如果您想检查用户是否保存在会话中,请尝试这样。
if (Auth::check())
{
echo 'My user is logged in';
}
It is better if you can provide all routes you are using for that.
如果您可以提供为此使用的所有路线,那就更好了。