cancel
Showing results for 
Search instead for 
Did you mean: 

Invalid security or form key. Please refresh the page.

Invalid security or form key. Please refresh the page.

I get the following error on the production server, while locally everything works fine

 

main.DEBUG: Request validation failed for action "Magento\Framework\App\Action\Forward\Interceptor" {"exception":"[object] (Magento\\Framework\\App\\Request\\InvalidRequestException(code: 0): Invalid request received at /var/www/html/magento/vendor/magento/module-backend/App/Request/BackendValidator.php:152)"} []

 

thanks

Pietro

4 REPLIES 4

Re: Invalid security or form key. Please refresh the page.

@dilorenzopd73c 

 

Can you share after which process you are getting this ?
also if new code is deployed or new module is added after testing local please run following commands :

php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:flush

Please run above commands and if issue still persists let us know the process after you got this.

Problem Solved ? Click on 'Kudos' & Accept as Solution to encourage to write more answers !

Re: Invalid security or form key. Please refresh the page.

I indicate the flow after executing the commands

 

php bin/magento setup:upgrade

php bin/magento setup:di:compile

php bin/magento cache:flush

 

we get a 500 error

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

 

By executing the command again

php bin/magento setup:di:compile

 

we receive the following reply

 

[] main.ERROR: Unable to load theme by specified key: 'Magento\backend' [] []

[] main.CRITICAL: Unable to load theme by specified key: 'Magento\backend' {"report_id":"29b6a6e5ab1b663d514fa42e4e6e4f4ee17864f9ba7512906f1978946a62a17b","exception":"[object] (LogicException(code: 0): Unable to load theme by specified key: 'Magento\\backend' at /var/www/html/magento/vendor/magento/framework/View/Design/Theme/FlyweightFactory.php:69)"} []

 

Then I run the commands to be able to restore the administration panel view

php bin/magento setup:static-content:deploy -f

php bin/magento cache:clean

 

clicking on the link I always get an “Invalid security or form key. Please refresh the page. "

Re: Invalid security or form key. Please refresh the page.

It's because you have validation with the form key.

If your request is for ajax use isajax=true in your call.

If not use the below function in your action PHP file

protected function isValidPostRequest()
{
$isPost = $this->getRequest()->isPost();
if(!$isPost) {
return true;
}
}


if your post the request you can also use it something similar to below to validate for key

protected function isValidPostRequest()
{
$formKeyIsValid = $this->_formKeyValidator->validate($this->getRequest());
$isPost = $this->getRequest()->isPost();
return ($formKeyIsValid && $isPost);
}

 

Don Udugala

Re: Invalid security or form key. Please refresh the page.

Some of the data security protection can save up our important data from unauthorized access. But still, most of the students can get find more where the importance of cybersecurity challenges facing higher education programs, and this can be good for learning the tricks for getting safe through cyber security.