Magento modules Blog 3
Magento session validation setting causing trouble when use_http_user_agent is enabled.
Magento session validation setting causing trouble when use_http_user_agent is enabled.
How a default Magento session validation flag silently killed GeoIP store switching, cost us untold international sales, and took 15 seconds to fix.
For seven years, our Magento store had a problem we couldn’t pin down. Customers would report landing on the wrong regional version of our site. They’d see the wrong currency, the wrong pricing structure, and a checkout flow meant for another country. Some figured out the store switcher and corrected it themselves. Many likely left.
We run two Magento 2 store views — one as the default and one for another country — with a GeoIP-based auto switcher routing visitors to the correct store based on location. Everything was configured correctly. The database was current. The mapping was correct. And yet, intermittently, customers would land on the wrong store.
Magento system logs showed two recurring errors appearing at the same timestamps:
The "http_user_agent" session value is invalid. Verify and try again.
Corrupt redirect cache entry intercepted, returning empty data
For years we treated these as unrelated warnings. They weren’t.
When a visitor lands on a Magento store with GeoIP switching enabled:
To avoid redirecting on every page load, a session flag is stored indicating the visitor has already been routed.
This works perfectly — as long as the session survives.
Magento includes a configuration setting:
web/session/use_http_user_agent
Enabled by default, this setting stores the visitor’s browser user agent in the session. If the user agent changes, Magento invalidates the entire session.
User agent strings change for completely normal reasons:
When this happens, Magento destroys the session.
For carts, it’s annoying. For store switching, it’s catastrophic.
When the session is invalidated:
Modern Magento storefronts fire multiple AJAX requests on load. These compete for session locks.
If lock contention delays the redirect, the 10-second cache token expires. The redirect fails silently. Visitors fall back to the default store view.
The bug appears random because it depends on timing.
bin/magento config:set web/session/use_http_user_agent 0 bin/magento cache:flush
Disable user agent validation. Sessions persist. The store switcher triggers once. The redirect race condition disappears.
After deployment, session validation errors stopped immediately. Redirect failures stopped. The issue was gone.
Validating the user agent provides minimal security benefit. User agents are trivial to spoof. But invalidating sessions under normal browsing conditions breaks:
If you use GeoIP store switching and customers occasionally land on the wrong store view, check this configuration first.
Intermittent bugs are the hardest to solve. Each system was functioning correctly in isolation. The failure existed in the interaction between them.
Seven years. One configuration flag.
Posted on