Why I keep getting Error 419 Page expired - laravel

I am using laravel 7 and keep having this issue in my form:
<form class="form-inline " role="form" method="POST" action="{{route('auth.signup')}}">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<div class="form-group">
<input type="email" class="form-control" id="inputEmail" name="email" value="email">
</div>
<div class="form-group">
<input type="password" class="form-control" id="password1" name="password">
</div>
<div class="form-group">
<input type="password" class="form-control" id="password2" name="password_confirmation">
</div>
<button type="submit" class="btn btn-primary mx-auto">Sign Up</button>
</form>
i already have changed config/session.php This line 'lifetime' => env('SESSION_LIFETIME', 1200),
Is it necessary to show route / controller they all just basic
I have also tried to remove the controller's entire code and simply dd("signed up") but i keep getting the same issues
I also went on to change my php.ini file but the problem persists.
maybe another piece of information to add is that the homepage which has the signup link to the form above also has its own form with a and its not the same token as the one in the form above
The sign up controller:
public function postSignup(Request $request)
{
$this->validate($request,[
'email'=>'required|unique:appscheduler_users|email|max:255',
'name'=>'required|max:255',
'password'=>'required|confirmed|min:6',
]);
$user=user::create([
'email'=>$request->input('email'),
'name'=>$request->input('name'),
'password'=>bcrypt($request->input('password')),
'created'=>date('Y-m-d H:i:s'),
'ip'=>$request->ip(),
]);
Auth::login($user);
if(Session::has('oldUrl')) {
$oldUrl=Session::get('oldUrl');
Session::forget('oldUrl');
return redirect()->to($oldUrl);
}
return redirect()
->route('home')
->with('info','Your Account has been created');
}

To solve this error you first need to insert one of the following commands into the form tag.
#csrf OR {{ csrf_field }}
If your problem is not resolved, do the following: and keep the csrf tag
1.Insert one of the following commands into the form tag #csrf OR {{ csrf_field }}
2.Open the .env file and change the values ​​to the "file" in the SESSION_DRIVER section.
3.Then you should reset laravel cache. type below commands in the terminal
php artisan view:clear php artisan route:clear php artisan cache:clear
php artisan config:cache
4.In the final step, unplug the project from the serve and click again on php artisan serve
I hope your problem is resolved
maybe you wanna take a look here
Post request in Laravel - Error - 419 Sorry, your session has expired

Related

Why am i getting an error 404 when i run this code in laravel 9?

I was watching an old tutorial about laravel 7 whiles using laravel 9, i tried to create a HTML form like this.
<div class="card-body">
<form action="/upload" method="post" enctype="multipart/form-data">
#csrf
<input type="file" name="image">
<input type="submit" value="upload">
</form>
</div>
then in my route(web.php) i added a code like this
route::post('/upload', function(Request $request)
{$request->image->store('images', 'public');
return 'image uploaded succesfully';
but in my webiste it tells me page the url you requested is not found on the site serve
You've defined your route using POST meaning it will only respond to POST requests.
If you try to access /upload from a web browser, that uses a GET request which you've not defined a route for. So you want to define such a route:
Route::get('/upload', function () {
return view('upload.blade.php');
});
You'll want to replace upload.blade.php with the name of your view that has your upload form in it.
Name Your Route 1st , hit this command php artisan route:clear then try..
Change the form action action="{{ route('upload') }}"
<div class="card-body">
<form action="{{ route('upload') }}" method="post" enctype="multipart/form-data">
#csrf
<input type="file" name="image">
<input type="submit" value="upload">
</form>
</div>

Laravel: something is changing the method, what is it?

I have a simple blade form mith post method and csrf token:
<form name="login" action="{{ route('login.do') }}" method="post" autocomplete="off">
#csrf
<label>
<span class="field icon-envelope">E-mail:</span>
<input type="email" name="email" required/>
</label>
<label>
<span class="field icon-unlock-alt">Password:</span>
<input type="password" name="password_check"/>
</label>
<button class="gradient gradient-orange radius icon-sign-in" type="submit">Send</button>
</form>
and the route in routes/web.php:
Route::post('login', 'AuthController#login')->name('login.do');
when I submit the form, I get the error:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods: POST.
As you can see, the route and the form are ok, but something is messing it up and changing the method. It won't even get to the controller layer.
some infos:
-Laravel 8.12
-I have no customized middleware
-The htaccess and config/cors are default
-The route doesn't end with a slash nor is duplicated
-Whenever I do an alteration I clean the caches
-The APP_URL starts with http and SESSION_SECURE_COOKIE is set to false due to it
-The APP_ENV is production
What can it be? I've read it may be something in the server that's blocking the request and changing its method, so I'm not sure if the error is laravel or server related.
public function login(Request $request){
dd($request->all());
}

“The page has expired error 419” - Laravel 7

My register page is showing the form properly with CsrfToken ({{ csrf_field() }}) present in the form).
<form method="post" action="{{route('products.store')}}">
{{csrf_field()}}
{{ method_field('post') }}
<div class="form-group">
<label for="exampleName">Name</label>
<input type="text" class="form-control" name="name" id="exampleName" placeholder="Enter Name" aria-describedby="emailHelp">
</div>
<div class="form-group">
<label for="exampleDescription">Description</label>
<input type="text" class="form-control" name="desc" id="exampleDescription" placeholder="Enter Name" aria-describedby="emailHelp">
</div>
<div class="form-group">
<label for="exampleWeight">Weight</label>
<input type="text" class="form-control" name="weight" id="exampleWeight" aria-describedby="emailHelp">
</div>
<div class="form-group">
<label for="examplePrice">Price</label>
<input type="text" class="form-control" name="price" id="examplePrice" aria-describedby="emailHelp">
</div>
<button type="submit" class="btn btn-success">Submit</button>
</form>
When I submit the form (just after reloading also), it gives that The page has expired due error 419. Please refresh and try again. error.
I very tried but cannot solve it
Please help me, thanks a lot
This article may help you:
https://medium.com/#tiwarishani/how-to-solve-the-page-expired-419-error-in-laravel-69fd97b0aa71
1.
Are you in production? using https?
I had the same problem and was not CSRF token. So I changed AppServiceProvider.php boot() method (which was empty before in my case) for this:
public function boot(UrlGenerator $url)
{
Schema::defaultStringLength(191);
if (env('APP_ENV') !== 'local') {
$url->forceScheme('https');
}
}
Don't forget this line at top:
use Illuminate\Routing\UrlGenerator;
** About using env() outside config files, be sure about executing
php artisan config:clear
Before testing
2.
You need to have write permission to the storage/framework/sessions folder (755)
execute
chmod 755 storage/framework/sessions
chmod 755 bootstrap/cache
chmod 644 vendor (not very sure about this)
SESSION_DOMAIN env variable could be important.
php artisan session:table
Go to env file and change
SESSION_DRIVER=file to ---> SESSION_DRIVER=database
In config directory in session.php file
'driver' => env('SESSION_DRIVER', ‘file') ---> 'driver' =>
env('SESSION_DRIVER', 'database')
php artisan cache:clear
php artisan config:cache
php artisan serve
finish.
have a good time

Laravel 5.7 tokenmismatch error when trying to post form

I'm running laravel 5.7, I changed the sessions to the database. I did make:auth and tried to register and got an error page with
"The page has expired due to inactivity. Please refresh and try again"
I have been at this for a couple of days now. I added /register to the except array to bypass the verify token and was able to register. Now I'm trying to login and get the same issue. I have **cleared cache, checked settings, switched back to file, tried other browsers, verified the token was in the form, make sure it was in the header, made sure it was getting passed and still nothing. I added this to the Handler class in Exceptions:
if ($exception instanceof \Illuminate\Session\TokenMismatchException) {
return redirect()->back()->with('error_message',"Oops! Seems there was an error with login. Please try again or contact us.");
}
and this leaves me to believe the token is not matching because it redirects instead of going to the broken page. I have read through I think every article on the internet and cannot figure this out. My next option is to start fresh with a new install unless anyone has any idea what could cause this?
This was on my local. I have not pushed anything to production.
Edited to add Form:
{!! Form::open(['route' => 'login', 'method' => 'post']) !!}
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-12">
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required autofocus>
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-12">
<input id="password" type="password" class="form-control" name="password" required>
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 offset-md-4">
<div class="checkbox">
<label>
<input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-8 offset-md-4">
<button type="submit" class="btn btn-primary">Login </button>
<a class="btn btn-link" href="{{ route('password.request') }}">
Forgot Your Password?
</a>
</div>
</div>
{!! Form::close() !!}
Try using a different browser to login or an incognito window.
Also remember on any form where you are POST-ing to use the #csrf in your blade file.
This problem comes from CSRF token validation which fails:
'The page has expired due to inactivity. Please refresh and try
again.'
you should include a hidden CSRF token field in the form so that the CSRF protection middleware can validate the request.
<form method="POST" action="/profile">
#csrf
...
</form>
It doesn't work, then Refresh the browser cache and now it might work
There should not accure any errors, so if you only want to create the auth scaffolding I would suggest just to create a new laravel project.
Do following steps:
laravel new app
create the database
Add the database credentials to your .env file
run php artisan:migrate in your command line, inside your project, that should create you a users table
run php artisan make:auth, that is going to create the basic auth logic
Lastly run php artisan migrate again
In case someone in the future comes here and tries all the solutions out there and they dont work I wanted to share what finally worked for me. I started by installing a new instance of laravel 5.8. I did make:auth and checked that the standard forms works as expected. I then added in the code from the instance that was not working which was basically my custom app.blade.php, header.blade.php,footer.blade.php,welcome.blade.php register.blade.php, helpers.php ( contains my custom helper functions ) and updated composer.json with the packages I use. I ran composer update and it did not work. I went back to the beginning and added each piece individually until I found the issue. I am not sure why this happened but the issue was this:
I have a custom file within app/ called helpers.php. I add it via composer.json like this:
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
},
"files": [
"app/Http/helpers.php"
]
},
I have one function in there that I use to wrap files in that adds in my aws S3 endpoint. Thats it. But for some reason this was causing the site to redirect on every page load back to itself and give me a new session. I would refresh over and over and just watch the session files accumulate. I read that the helper functions are getting deprecated in 5.9 so I went ahead and created a provider then use that in place of my helper function which did the trick.
If someone knows why this happens I would love to hear it. But if you have this issue, open up the sessions folder in your editor and see if you keep getting new sessions. If so then backtrack and see whats causing it to reroute.

Laravel Use of undefined constant in form

Hello so I am a beginner in laravel and having some problems. I am not using Illuminate html for my forms because I want just plain html forms. I'm getting this Use of undefined constant id - assumed 'id' in my edit.blade.php. Here is my edit.blade.php:
<form action="/books/{{$book.id}}/update" method="POST">
Title: <input type="text" name="title"> <br/>
Author: <input type="text" name="author"> <br/>
ISBN: <input type="text" name="isbn"> <br/>
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit">
</form>
And in my controller:
public function getBook($id) {
$book = Books::findOrFail($id);
return view('books.edit', compact('book'));
}
Am I doing something wrong?
Ok so I fixed it by myself.
First thing is I changed the line {{$book.id}} to {{$book->id}}.
Second is to edit .env with this values:
CACHE_DRIVER=array
SESSION_DRIVER=cookie
QUEUE_DRIVER=array
Then restart server php artisan serve.

Resources