a-ok Posted December 30, 2019 Share Posted December 30, 2019 (edited) I'm attempting to include some code in the head.inc file that will check if the page is available in the user's location language and if so it'll redirect to that page. Obviously this isn't a great for UX but alas my client is keen. The only issue I need to overcome is if the user has already been redirected then don't do it for every page... store the fact that the user was redirected and don't do it again until the session has ended. Something like that. I have written the code below but a) I'm unsure this is the best way to do it and b) it seems to get stuck in a redirect loop (which I thought I'd avoided by my second session check). I'm using the ipapi.com to check the user's location. Any thoughts? if (!$session->get("lucentLightingRegion")) { bd($page->ipapiGetUserLocation()); $session->set("lucentLightingSessionRedirected", "false"); if ($page->ipapiGetUserLocation()['continent_code'] === 'NA') { $session->set("lucentLightingRegion", "NA"); if ($page->viewable('na')) { $url = $page->localUrl('na'); $session->redirect($url); } } else { $session->set("lucentLightingRegion", "INT"); if ($page->viewable('default')) { $url = $page->localUrl('default'); $session->redirect($url); } } } else { $sessionRegion = $session->get("lucentLightingRegion"); bd($sessionRegion); if ($page->viewable($sessionRegion) && $session->get("lucentLightingSessionRedirected") == "false") { $url = $page->localUrl($sessionRegion); $session->redirect($url); $session->set("lucentLightingSessionRedirected", "true"); } } Edited December 30, 2019 by a-ok Link to comment Share on other sites More sharing options...
a-ok Posted January 4, 2020 Author Share Posted January 4, 2020 ? Link to comment Share on other sites More sharing options...
dragan Posted January 5, 2020 Share Posted January 5, 2020 On 12/30/2019 at 11:02 PM, a-ok said: $session->redirect($url); $session->set("lucentLightingSessionRedirected", "true"); Just a thought: Shouldn't you be setting the session first, and then redirect? (change order) Your $session->set() is probably never executed at all. On 12/30/2019 at 11:02 PM, a-ok said: == "false" Also maybe worth checking out: perhaps you need a === instead of ==. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now