我正在使用Yellowtree的GEOIP-Detect插件,以便根据访问者的位置将访问者重定向到页面。然而,我不能让代码完全工作。代码首先获取用户的IP,然后将IP中的信息存储在多维数组中。变量$statecode提取出列出的状态,然后根据提取的状态数组调用切换。
然后,我转到Javascript,以便尝试将访问者从当前页面重定向到新页面。该脚本迁移到PHP变量上,以便它可以在JS中正确读取,然后一个函数尝试使用window.location.replace重定向页面,这是我读到的只能在Chrome中工作的内容,然后作为其他浏览器的故障安全window.location。但是,什么也不会发生。我哪里错了?
<?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
}
}
?>发布于 2020-01-04 12:24:51
您可以通过发送Location头直接在PHP中进行重定向。请注意,您需要在$url前面加上http://,这样它就不会被视为当前URL的相对路径。
if ($url) {
header("Location: http://$url");
exit;
}https://stackoverflow.com/questions/59587815
复制相似问题