Magento 1: Invalid Form Key. Please refresh the page, admin login

Table of Contents

If you use vagrant to develop magento ecommerce website on a windows machine, you may have invalid form key issue.

magento_formkey_error

If you have enabled system log, you will see this similar error report:

ERR (3): Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/vagrant/htdocs/var/session)  in Unknown on line 0

This error causes your customer cannot login or you cannot login to backend system, because session handler cannot write data to session storage. If you check your var/session folder, you will see list of files with empty data similar as mine

session_files_empty
You should update your session setting in your local.xml file

<session_save_path>

<![CDATA[/tmp/session]]>

</session_save_path>

After that, please remember to clear cache and your browser history, you will have your magento login system worked again. Run

ls -la

inside

/tmp/session

give me this output

session_files_stored_correctly

It proved that session stored correctly.

I met this problem with this version of PHP7, Zend OPcache v7.0.10-2+deb.sury.org~trusty+1

if fixing the permission does not sort out the problem, it might come from cookie domain setting, this may happen when

you migrate your store from this domain to another domain. You should simply remove that row from core_config_data table

by using this query, and you can login into admin successfully

DELETE FROM core_config_data WHERE path='web/cookie/cookie_domain';
DELETE FROM core_config_data WHERE path='web/cookie/cookie_path';