Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get session remember after login in laravel 5?

I start to work with my project using laravel 5. I realized that it work fine in my local directory with session after I login to my site, But I just know that I got problem when I hosted my project to server. After I login each time, the session could not remember that cause the problem to me.

public function postLogin()
{
    $hit = 0;

    if(Request::ajax()){
        $pw = Request::input('pw');
        if(!empty($pw)){
            $admin_pass = AdminPassword::first(['admin_pass']);
            $ip_address = Request::ip();
            if(!empty($admin_pass) && trim($admin_pass->admin_pass) == trim($pw)){
                if(Auth::attempt(['username' => Request::input('username'), 'password' => Request::input('password'),'status'=>1])){
                    try{
                        $user = Auth::user();
                        $user->last_login = date('Y-m-d H:i:s');
                        $user->login_ip = $ip_address;
                        $user->save();
                        $permissions = Auth::user()->permission;
                        if(!empty($permissions) && count($permissions) >0){
                            session(['ROLE_PERMISSION'=>$permissions]);
                        }
                        $failed = FailedLogin::whereRaw('ip_address = INET_ATON(\''.$ip_address.'\')')->first();
                        if(!empty($failed)){
                            $failed->delete();
                        }
                    }catch (\Exception $ex){}
                    $url = Request::session()->pull('url.intended', '/');
                    return ['url'=>$url,'msg'=>'Successfully.','status'=>true,'hit'=>$hit];
                }else{
                    $hit = $this->updateFailedLogin($ip_address,Request::input('username'));
                }
            }else{
                $hit = $this->updateFailedLogin($ip_address,Request::input('username'));
            }
        }
    }else{
        return redirect()->route('login');
    }
    return ['url'=>'','msg'=>'Try again.','status'=>false,'hit'=>$hit];
}

Please help me out from this.

Thank you very much.

like image 213
Tum Lina Avatar asked Jul 20 '15 03:07

Tum Lina


People also ask

How do you enable remember me in Laravel?

In your App\Http\Controllers\LoginController. php we will change our login method and implement the remember me. As you can see above I added $request->get('remember') to the second parameter of Auth::login() function.

How can get all session in Laravel?

If you just want to see contents of session, try dd() : dd(session()->all()); If not, just use this to get all info: $data = session()->all();

Where sessions are stored in Laravel?

file - sessions will be stored in storage/framework/sessions . cookie - sessions will be stored in secure, encrypted cookies. database - sessions will be stored in a database used by your application.


5 Answers

If this works perfectly locally and not only its probably one of two things. 1) you need to set permissions on your storage folder to be writable. 2) when you login locally on the browser and it saves your cookie, if you try to log in online it logs you out locally because its the same cookie basically but point to different domains. So you probably should use different browsers if you want to work locally and online at the same time

like image 98
Kaspersky Avatar answered Oct 04 '22 21:10

Kaspersky


Use below code

 setInterval(function(){
    $.get('/my/ping/route');
 }, 1000 * 60);

.ajaxError()
like image 31
Gowthaman D Avatar answered Oct 04 '22 21:10

Gowthaman D


What you can try is Session:save(); It solved my problem where session has not been carried over when redirecting user to payment gateway.

From what I heard it is caused by some interruption in HTTP request.

like image 20
Lixi Avatar answered Oct 04 '22 19:10

Lixi


If the error logs in storage/logs don't give any clues about issues such as directory permissions, check your .env file on your server for the SESSION_DRIVER variable that is loaded in config/session.php to make sure you have the right persistence mechanism based on your server configuration (file, cookie, database, apc, memcached, redis, etc.).

If that doesn't help, check the config/session.php file. I've seen someone set the lifetime property to 0 thinking that this would make sessions indefinite, but in reality it caused the issue that you're having.

like image 37
JasonBoss Avatar answered Oct 04 '22 19:10

JasonBoss


Please check how you are storing your session.

By default laravel uses file-based ( storage/framework/sessions ), make sure the directory is writeable.

like image 41
Kyle Yee Avatar answered Oct 04 '22 19:10

Kyle Yee