0

I have problems when submitting a form in Laravel application. It reported 419 error.

My code:

<form action="login" method="POST">
        <input id="csft_pass" type="hidden" name="_token" value="{{ csrf_token() }}">
        .....
</form>

I tried fixing it:

<form action="login" method="POST">
        @csrf
        .....
</form>

But still, error 419

With the above code still running normally, suddenly there was an error today

I tried many ways like php artisan cache:clear but still not solve the issue.

My Laravel version: 5.8

UPDATE: I tried a lot of solutions on stackoverflow but still can't solve it. I think that because the application's session has something wrong

Ajax
  • 77
  • 1
  • 8

4 Answers4

1

After form tag use csrf_field.

{{ csrf_field() }}

And if you are using ajax you may pass csrf token on meta tag like.

<meta name="csrf-token" content="{{ csrf_token() }}">
Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49
iAmGroot
  • 950
  • 1
  • 6
  • 22
  • I tried it but still got an error, the problem is that this code still works fine before. Thanks for comment! – Ajax Aug 02 '19 at 05:35
0

You can use the csrf_field helper to generate the token field:

<form method="POST" action="/login">
    @csrf
    ...
</form>

OR

<input type="hidden" name="_token" value="{{ csrf_token() }}">

It doesn't work, then Refresh the browser cache and now it might work.


Why required: Refresh the browser cache

When we update our application, a browser may still use old files. If you don’t clear your cache, Old files can access problems when you apply.


For more details open link :- Error - 419 Sorry, your session has expired

Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
0

With the above code still running normally, suddenly there was an error today

This makes me suspect the error occurs only when the form was opened for more than two hours (that's the default of lifetime in config/session.php) before submitting.

If that's the case, you could set a value of more than 120 minutes as lifetime or do something in frontend to keep the session alive, such as some custom JavaScript (for single forms) as described in the selected answer to this thread or Laravel Caffeine (for whole apps)

Pida
  • 928
  • 9
  • 32
  • What does not work? Is it true the error only occurs when the form has been opened for more than two hours? Did you implement both of the approaches mentioned in my answer? – Pida Aug 02 '19 at 08:00
0

Replace these lines <input type="hidden" name="_token" value="{{ csrf_token() }}">

Dasun Tharanga
  • 163
  • 1
  • 11