Laravel page expired 419 only sometimes - laravel

I have a view in my Laravel application that has a form with the #csrf inside. The problem is that I faced the 419 Page expired exception only sometimes. When I refreshed the page, the login page appeared and after login, a few times the form submission was done successfully but again, 419 occurred.
I saw the other topics. All of them told that the solution is adding the #CSRF directive to the form.
But in my special case, the #CSRF exists.
Help me please.
Update
Here is a part of my source code. It's a simple html form with the #CSRF inside it.
<form method="POST" action="/settingUrl">
#csrf
<input type="text" name="myfield" />
<input type="submit" value="save" />
</form>

Related

AMP form issue in a MVC framework

I am having an issue with a form using AMP.
The form works perfectly on a standard amp html page - the button is clicked and the search.php is triggered.
<form method="post" action-xhr="search.php" target="_top">
<input name="query" placeholder="search" id="query" size="40" value="">
<button type=submit class="btn-search"><amp-img ></amp-img></button>
<input type=hidden name=search value=1>
</form>
Then the exact same code (including all the includes, etc) on the MVC framework I am using doesn't work. The button is clicked and nothing happens.
Source codes on both are identical when viewing page source.
Should this not be happening - the specific MVC framework I am using possibly has an issue with this?
Or is it due to it being MVC, the view (header.twig) has the form on the not working version. Would the php code from search.php go into the controller?
Any help would be greatly appreciated!

Laravel - 419 issues when Submit Form

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
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() }}">
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
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)
Replace these lines
<input type="hidden" name="_token" value="{{ csrf_token() }}">

I am getting 404 not found error while the route exisits

I am using Laravel to build my todo app.
In the web.php routes file i have added this route:
Route::put('/tasks/changecat', 'TaskController#changeCat');
and i am calling this route from a form in a .blade.php file like so:
<form action="tasks/changecat" id="change-cat-form" class="d-none" method="POST">
#method('PUT')
#csrf
<input type="text" name="task" id="task-input">
<input type="text" name="category" id="category-input">
</form>
But when i try to submit the form on the browser i get 404 not found
I tried to use postman and i have included the csrf token in the headings, i get a 200 ok but i get redirected to the login page.
what do you think it's causing the problem?
I fixed this when i changed the method to PATCH.
Route::patch('/tasks/changecat', 'TaskController#changeCat');
It turns out that i should use PATCH because i needed to change a part of the resource and not all of it.
Using PATCH will change the 'updated_at' column value automatically too.

AJAX call to PHP not working yields a blank page

We are implementing the "forget password" feature for our website.
We are usign HTML, JQquery mobile and AJAX .
we have a main file, which has the hyperlink for the forget password using the '' tag.
The password.html has form elements as below:
<form method ="post" id="forget" action="somefile.php" >
<h4>Enter your login email to change your password..</h4>
<label for="email" class="ui-hidden-accessible">Email:</label>
<input type="email" name="email" id="email" value="" placeholder="email" data-theme="a" />
<input type="submit" value="Submit" data-theme="b" />
</div>
</form>
The php file validates the email and sends the response in JSON format , which we are handling through a ajax call.
The problem is after hitting on "submit" in the password.html, we are able to retreive the data from the php file(able to see in network tab of browser) but it does not display on the frontend.
The browser is navigating to the php file (which it should not) and a blank page is being displayed.
Ps: When we directly load the password.html in the browser the behaviour is absoultely fine , but when we are navigating from the main file usign the hyperlink to password.html then this problem occurs.

spring security CSRF protection

I read in spring documentation that logout need to be submitted using post method.Something like :
<c:url var="logoutUrl" value="/problemSolution/logout"/>
<form action="${logoutUrl}" id="logout" method="post">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</form>
Logout
Why is it so ?
Because if it's a GET then someone else can log you out, which is annoying.
You go to compromised.com
They run a script that makes a GET request to yourbank.com/logout
You are logged out of yourbank.com

Resources