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

mornenel79's avatar

Server Error 500 when VerifyCsrfToken is swiched off

I have been getting this error and to get rid of it I changed the following.

In VerifyCsrfToken.php I added the following to allow any route

protected $except = [
	'*'
 ];

Also in Kernel.php I commented out this:

// \App\Http\Middleware\VerifyCsrfToken::class,

But I still get the dreaded { message: "Server Error" } Not very often, but it happens. It always happens with the same items but the turnaround for those requests are about 180ms, so quite quick. There are about 9 requests that go out at the same time and the respons always fails on the 7th or 9th one, never the 8th ( for some reason ).

Im using React 0.13 ( Im stuck on this version for now ) and Ajax. ( React 0.13 cant use Axios ) Before the changes above I had the following code in bootstrap.js

 $.ajaxSetup({
    headers: {
    	'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
    	'X-Requested-With': 'XMLHttpRequest'
    }
 });

and the following in my Start page:

<meta name="csrf-token" content="{{ csrf_token() }}">

But even with this I got { message: "Server Error" } very frequently.

Im not sure what other information I missed, but please can someone shed some light on this topic for me.

0 likes
21 replies
mornenel79's avatar

Im using Laravel 7.4 Recently upgraded from 5.2 Did not experience any of this on 5.2.

Sinnbeck's avatar

Did you check your logs for what error is thrown? (laravel.log)

mornenel79's avatar

This is what I get.

[2020-08-13 11:02:28] production.ERROR: No application encryption key has been specified. {"exception": "[object] (RuntimeException(code: 0): No application encryption key has been specified. at C:\\Acute\\vendor\\laravel\\framework\\src\\Illuminate\\Encryption\\EncryptionServiceProvider.php:80) [stacktrace]

I did run php artisan key:generate previously and added the key to my .env file.

Sinnbeck's avatar

Is this a production server or local development? Is the config cached?

mornenel79's avatar

Its in testing at the moment, so local development I'd say.

Snapey's avatar

Nothing to do with csrf....

Try with and without caching of the config

mornenel79's avatar

I have now run php artisan cache:clear php artisan route:clear php artisan config:clear php artisan view:clear

and the browser cache.

mornenel79's avatar

Ah No. After running the artisan commands. I just nog got the exact same error.

[2020-08-13 11:02:28] production.ERROR: No application encryption key has been specified. {"exception": "[object] (RuntimeException(code: 0): No application encryption key has been specified. at C:\\Acute\\vendor\\laravel\\framework\\src\\Illuminate\\Encryption\\EncryptionServiceProvider.php:80) [stacktrace]

Sinnbeck's avatar

Can you check your .env file? Does it have something like this?

APP_KEY=base64:eLnjbQPAZUMmHFxk4rBGWosZ2IhJCNV+G6ns1mSeQ2o=
mornenel79's avatar

APP_ENV=production APP_DEBUG=false DB_DATABASE=OR DB_USERNAME=postgres APP_KEY=base64:Igtp2T9Sx3yvXfxwqt5lVtjpStJLj/JbOSecldAL6A0=

This it the whole content of .env

mornenel79's avatar

APP_ENV=production

APP_DEBUG=false

DB_DATABASE=OR

DB_USERNAME=postgres

APP_KEY=base64:Igtp2T9Sx3yvXfxwqt5lVtjpStJLj/JbOSecldAL6A0=

mornenel79's avatar

This is a patient admission application.

The strange thing is that it works fine and then on admitting something like the 10th patient I get this, but only on one request.

Then its fine again and then again server error, but this time on another request.

Sinnbeck's avatar

Does it make a difference if you set it to local and enable debug?

APP_ENV=local

APP_DEBUG=true
mornenel79's avatar

Does including it in the config.php file make any significant difference?

Used to look like 'key' => env('APP_KEY'),

Now changed it to look like

'key' => env('APP_KEY', 'base64:2+GtwJ+zL8Bw/QXqWaNcywnhdZ2edrhh2w8OdOvtloc='),

mornenel79's avatar

ot it should rather be

'key' => ('base64:2+GtwJ+zL8Bw/QXqWaNcywnhdZ2edrhh2w8OdOvtloc='),

Sinnbeck's avatar

That would only be for debugging purposes. Do not use that as a solution

mornenel79's avatar

@sinnbeck Do you perhaps have any other idea why it would come up with this error even after the changes and commands tried?

Sinnbeck's avatar

No I have never seen that error like that before. But did you try it with local and debug on? Did it make a difference?

mornenel79's avatar
mornenel79
OP
Best Answer
Level 1

I could not run php artisan config:cache becasues of 2 files with incorrect namespaces. When I fixed that issue I was able to run that command and I dont get the Server Error or No application encryption key has been specified. anymore

Please or to participate in this conversation.