Magento 2 Your session has expired

After migrating Magento 2 site to a new server with a different URL, adding an item to the cart produced the error message “Your session has expired”.

First, I verified PHP session is working with a test script


Renamed index.php to index.php.old and created a new index.php file with the above code. Every time I refreshed, I get a new number, which meant PHP session is working. If the number stay same on refresh, PHP session is not working, you need to look at your PHP configuration, and verify the session.save_path settings in php.ini is set properly and is writeable.

Verify URLs are set properly.

select * from core_config_data where path like '%base_url';

If you see wrong URL, update it with the command

UPDATE core_config_data SET value = 'https://your-domain.tld/' WHERE path = 'web/unsecure/base_url';
UPDATE core_config_data SET value = 'https://your-domain.tld/' WHERE path = 'web/secure/base_url';

Also verify cookie settings, main domain is set properly.

select * from core_config_data where path LIKE '%cookie%';

Example

mysql> select * from core_config_data where path LIKE '%cookie%';
+-----------+---------+----------+-------------------------------------------------+----------------------+---------------------+
| config_id | scope   | scope_id | path                                            | value                | updated_at          |
+-----------+---------+----------+-------------------------------------------------+----------------------+---------------------+
|        42 | default |        0 | web/default/cms_no_cookies                      | 404-not-found        | 2020-03-03 13:13:18 |
|        49 | default |        0 | web/browser_capabilities/cookies                | 0                    | 2020-03-03 13:13:18 |
|      1432 | default |        0 | newsletter/general/cookiename                   | es_newssubscribers   | 2020-03-03 13:13:18 |
|      1433 | default |        0 | newsletter/general/cookielifetime               | 999                  | 2020-03-03 13:13:18 |
|      2268 | default |        0 | system/external_page_cache/cookie_lifetime      | 3600                 | 2020-03-03 13:13:18 |
|      2645 | default |        0 | magepsycho_easypathhints/general/save_in_cookie | 0                    | 2020-03-06 05:15:10 |
|      3020 | default |        0 | web/cookie/cookie_path                          | /                    | 2020-08-31 13:36:11 |
|      3021 | default |        0 | web/cookie/cookie_domain                        | 5351499f87.nxcli.net | 2021-09-30 12:24:53 |
|      3022 | default |        0 | web/cookie/cookie_httponly                      | 1                    | 2020-08-26 12:29:36 |
+-----------+---------+----------+-------------------------------------------------+----------------------+---------------------+
9 rows in set (0.01 sec)

mysql>

Look for setting web/cookie/cookie_domain, if it is wrong domain, set it to the new domain or leave it empty. In this case, the domain was set to old URL - 5351499f87.nxcli.net

UPDATE core_config_data SET value='' WHERE path = 'web/cookie/cookie_domain';

Clear magento cache

php bin/magento c:c
php bin/magento c:f

Back to Magento

Need help with Linux Server or WordPress? We can help!

Leave a Reply

Your email address will not be published. Required fields are marked *