Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

KurtVH's avatar

Session flash message remains when going back using browser

I've been using this code to display a message:

@if (Session::has('message'))
    <div id="welkomBericht">
            {{ Session::get('message')}}
    </div>
@endif 

After clicking away from the page and returning to it using the back button of the browser, the message appears again.

I'm using the flash method of Session. Can I change this behavior?

Thx

0 likes
2 replies
rin4ik's avatar

try this with jquery . import jquery in head section of your template

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
@if (Session::has('message'))
    <div id="welkomBericht" class="fade-message">
            {{ Session::get('message')}}
    </div>

    <script>
    $(function(){
        setTimeout(function() {
            $('.fade-message').slideUp();
        }, 5000);
    });
    </script>
@endif 
jcphpdev's avatar

You can use flash method (Data stored in the session using this method will only be available during the subsequent HTTP request, and then will be deleted):

$request->session()->flash('status', 'Task was successful!');

In your view, use this :

@if (session('status'))
    <div class="alert alert-success">
        <strong>{{ session('status') }}</strong>
    </div>
@endif 

Please or to participate in this conversation.