“The page has expired error 419” - Laravel 7 - laravel

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

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>

Why I keep getting Error 419 Page expired

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

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.

Method not Allowed HTTP Exception

Here is the code of HTML Form
<form method="POST" action={{ route('store') }} class="col s12">
#csrf
<div class="row">
<div class="input-field col s6">
<input name="task" id="task" type="text" class="validate">
<label for="task">New Task</label>
</div>
</div>
#include('partials.coworkers')
<button type="submit" class="waves-effect waves-light btn">Add Task</button>
</form>
#isWorker
<br><br><br>
<form action="" class="col s6">
<div class="row">
<div class="input-field col s6">
<select>
<option value="" disabled selected>Send Invitation To</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<label>Send Invitation</label>
</div>
</div>
<a class="waves-effect waves-light btn">Send Invitation</a>
</form>
Here is my code for store method inside a controller.
public function store(Request $request){
if ($request->input('task')) {
$task = new Task;
$task->content = $request->input('task');
Auth::user()->tasks()->save($task);
}
return redirect()->back();
}
web.php:
Route::middleware(['auth'])->group(function(){
Route::get('/', 'CrudApp#index');
Route::get('/store', 'CrudApp#store')->name('store');
Route::get('/edit/{id}', 'CrudApp#edit')->name('edit');
Route::get('/update/{id}', 'CrudApp#update')->name('update');
Route::get('/delete/id', 'CrudApp#delete')->name('delete');
});
I want to store the task into database. as soon as I hit save MethodNotAllowedHTTPException occurs and i am unable to figure out how to resolve the issue.
First of all:
php artisan route:cache
make it after any changes in route file (web.php in your case).
And now your code. Look u use POST:
<form method="POST" action={{ route('store') }} class="col s12">
so change:
Route::get('/store', 'CrudApp#store')->name('store');
on:
Route::post('/store', 'CrudApp#store')->name('store');
Using below command you will get the List of Routes where you can find your route:
php artisan route:list
Then as #Adam answer :
php artisan route:clear
php artisan route:cache
And in your query I think you are using Resource Controller.
If you are using resource method then you dont have to write all route separately
You can write your route in one line also:
Route::resource('demo-segment', 'DemoController');
You can also skip the unneccessory method from your resource controller
Route::resource('demo-segment', 'DemoController', [
'except' => ['show', 'edit', 'update', 'destroy'] // This is not usable methods
]);
Thanks, might help someone.

laravel 5.2 greggilbert/recaptcha : array_merge(): Argument #1 is not an array

I installed greggilbert/recaptcha in my laravel 5.2 project. I followed the direction accordingly described here:https://github.com/greggilbert/recaptcha
But when I opened the form where I used recaptcha, I found the above message.
I read the source code but understand nothing.
Can anybody help me in this regard to find any solution?
Thanks in advance.
Here is my form HTML Code
#section('form')
{{ Form::open(array('url' => 'contact')) }}
<div class="panel-body">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-comment blue"></i></span>
<textarea name="InputMessage" rows="6" class="form-control" required placeholder="Message"></textarea>
</div>
</div>
{!! Recaptcha::render() !!}
<div class="">
<button type="submit" class="btn btn-info pull-right">Send <span class="glyphicon glyphicon-send"></span></button>
<button type="reset" value="Reset" name="reset" class="btn">Reset <span class="glyphicon glyphicon-refresh"></span></button>
</div>
</div>
{{Form::close()}}
#endsection
The Problem is solved.
The problem was:
php artisan vendor:publish --provider="Greggilbert\Recaptcha\RecaptchaServiceProvider"`
command did nothing.
So recaptcha.php file was not copied to root config folder.
I copied the recaptcha.php file from
vendor > greggilbert > recaptcha > src > config > recaptcha.php`
to config folder and the problem disappears.
After vendor publishing you need to clear config cache using command
php artisan config:cache

Resources