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

MARIN's avatar
Level 2

Laravel csrf session timeout

Hi,

For registering visitors at our company I've create a Laravel app. The registration form is displayed all day long. Sometimes it causes a csrf token mismatch on submit.

Is there any solution for this?

0 likes
2 replies
RamjithAp's avatar

A workaround for it is to actually get the new token every certain time. otherwise, you are defeating the purpose of the csrf token:

<html>
    <head>
        <meta name="csrf_token" content="{{ csrf_token() }}">
    </head>
    <body>
        <script type="text/javascript">
            var csrfToken = $('[name="csrf_token"]').attr('content');

            setInterval(refreshToken, 3600000); // 1 hour 

            function refreshToken(){
                $.get('refresh-csrf').done(function(data){
                    csrfToken = data; // the new token
                });
            }

            setInterval(refreshToken, 3600000); // 1 hour 

        </script>
    </body>
</html>

In laravel routes

Route::get('refresh-csrf', function(){
    return csrf_token();
});

Please or to participate in this conversation.