I have managed to redirect users based on their country code/IP using if/else statement.
<?php
if ($geoplugin['geoplugin_countryCode'] == 'US') {
print '<script language="Javascript">window.location="http://domain.com/us";</script>';
}
else
print '<script language="Javascript">window.location="http://domain.com/ca";</script>';
}
?>
This works. However, I have half dozen pages where visitors need to be redirected to based on their location.
How can I send users to various pages based on their locations?
I am very new to web development so please forgive me for asking a newb question.
You could try a switch statement. switch
in PHP - switch
in JavaScript (hint: they're the same)
You can use php switch
case, NOTE:I am not using break;
statement because using the break
statement will increase the code.Use this
<?php
switch($geoplugin['geoplugin_countryCode']){
case'us':
case 'uk':
echo '<script language="Javascript">window.location="http://domain.com/"'.$geoplugin['geoplugin_countryCode'].';</script>';
}
?>