重定向到子域名

huangapple go评论73阅读模式
英文:

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?

&lt;?php 
if (function_exists(&#39;geoip_detect2_get_info_from_current_ip&#39;)){
	$userInfo = geoip_detect2_get_info_from_current_ip();
	$stateCode = $userInfo-&gt;subdivisions-&gt;isoCode;
	switch ($stateCode) {
		case &#39;AL&#39;:
		$url = &#39;subdomain1.domain.tld&#39;;
		break;
		case &#39;AR&#39;:
		$url = &#39;subdomain2.domain.tld&#39;;
		break;
		default:
		$url = &#39;subdomain3.domain.tld&#39;;
	}	
	if ($url) { ?&gt;
	&lt;script type=&quot;text/javascript&quot;&gt;
		var url = &lt;?php echo $url ?&gt;;
		function(){
			try { window.location.replace(url); } 
			catch(e) { window.location = url; }
		}
	&lt;/script&gt;
	&lt;?php
	}
}	
?&gt;

答案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(&quot;Location: http://$url&quot;); 
    exit; 
}

huangapple
  • 本文由 发表于 2020年1月4日 12:13:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/59587815.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定