Search code examples
phpflashsessionlaravel-4message

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.


Solution

  • Wouldn't this be a beter fit?

    return View::make('superadmin::login')->with('message', 'Invalid credentials');
    

    and then in your view:

    @if($message)
        <div class="alert-box message">
            <h2>{{ $message }}</h2>
        </div>
    @endif
    

    This is because you need not to flash, as you never use a redirect after a flash.