Jump to content

Redirect to user language's page version and store


a-ok
 Share

Recommended Posts

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 by a-ok
Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...