Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4: Session Flash Message Not Disappearing on Page Refresh

I am using same method for get and post request of login authentication like

public function login()
{
    if (Request::isMethod('post')){
        // Getting credentials
        $username = Input::get('username');
        $password = Input::get('password');
        // Authenticating super admin
        if(Auth::attempt(array('email'=>$username,'password'=>$password,'sysadmin'=>1))){

            return Redirect::To('superadmin');
        }else{
            Session::flash('message', 'Invalid Credentials !'); 
            return View::make('superadmin::login');         
        }           
    }else{
        echo View::make('superadmin::login');
    }
}

This is the code of login view to display error messages:

@if(Session::has('message'))
    <div class="alert-box message">
        <h2>{{ Session::get('message') }}</h2>
    </div>
@endif

The problem is when i enter invalid credentials while login, then error message is displayed at the form but after that on manually refreshing the login page, session message does not disappear. Where as, on second time page refresh, session message disappears.

I know that this is happening because URL (referrer) remain same in my case because same method "login" is being used for both types of request (get and post).

What would be the best way to display error messages in this case so that on page refresh or automatically the message should disappear.

like image 732
neeraj Avatar asked Mar 20 '23 07:03

neeraj


1 Answers

This is an old question but I want to explain the easiest way:

@if(Session::has('message'))
    <div class="alert-box message">
        <h2>{{ Session::pull('message') }}</h2>
    </div>
@endif
like image 72
Hüseyin Dursun Avatar answered Mar 30 '23 01:03

Hüseyin Dursun