英文:
Redirect to a SubDomain
问题
以下是您要翻译的内容:
"I'm using Yellowtree's GEOIP-Detect plugin in order to redirect visitors to pages based upon their location. However I cannot get the code to completely work. The code first grabs the users IP and then the info from the IP is stored in a multidimensional array. The variable $statecode plucks out the state listed and then switch is called upon based upon what state array is pulled.
I then move over to Javascript in order to attempt to redirect the visitor from the current page to the new page. The Script migrates over the PHP Variable so that it can read correctly in JS and then a function attempts to redirect the page using window.location.replace which is what I read will only work in Chrome, and then as a failsafe window.location for other browsers. However, nothing happens. Where did I go wrong?
subdivisions->isoCode;
switch ($stateCode) {
case 'AL':
$url = 'subdomain1.domain.tld';
break;
case 'AR':
$url = 'subdomain2.domain.tld';
break;
default:
$url = 'subdomain3.domain.tld';
}
if ($url) { ?>
<script type="text/javascript">
var url = <?php echo $url ?>;
function(){
try { window.location.replace(url); }
catch(e) { window.location = url; }
}
</script>
<?php
}
}
?>
"
英文:
I'm using Yellowtree's GEOIP-Detect plugin in order to redirect visitors to pages based upon their location. However I cannot get the code to completely work. The code first grabs the users IP and then the info from the IP is stored in a multidimensional array. The variable $statecode plucks out the state listed and then switch is called upon based upon what state array is pulled.
I then move over to Javascript in order to attempt to redirect the visitor from the current page to the new page. The Script migrates over the PHP Variable so that it can read correctly in JS and then a function attempts to redirect the page using window.location.replace which is what I read will only work in Chrome, and then as a failsafe window.location for other browsers. However, nothing happens. Where did I go wrong?
<?php
if (function_exists('geoip_detect2_get_info_from_current_ip')){
$userInfo = geoip_detect2_get_info_from_current_ip();
$stateCode = $userInfo->subdivisions->isoCode;
switch ($stateCode) {
case 'AL':
$url = 'subdomain1.domain.tld';
break;
case 'AR':
$url = 'subdomain2.domain.tld';
break;
default:
$url = 'subdomain3.domain.tld';
}
if ($url) { ?>
<script type="text/javascript">
var url = <?php echo $url ?>;
function(){
try { window.location.replace(url); }
catch(e) { window.location = url; }
}
</script>
<?php
}
}
?>
答案1
得分: 2
你可以通过在PHP中发送一个Location
头来直接进行重定向。请注意,你需要在$url
前面加上http://
,以防它被视为相对于当前URL的路径。
if ($url) {
header("Location: http://$url");
exit;
}
英文:
You can do the redirection directly in PHP by sending a Location
header. Note that you need to prepend http://
to $url
so that it doesn't get treated as a relative path to the current URL.
if ($url) {
header("Location: http://$url");
exit;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论