【发布时间】:2023-03-20 04:48:01
【问题描述】:
目前我在默认位置有一个 Zend 应用程序
www.example.com/{controller}/{action}
但是当用户从特定国家访问时,如何检测他们的 IP 地址并将他们重定向到这个基于 countryCode 的 url
www.example.com/uk/{controller}/{action}?
为了检测用户访问的国家/地区,我编写了一个助手:
require_once '../library/Ipinfo/ip2locationlite.class.php';
class Application_View_Helper_GetLocation extends Zend_View_Helper_Abstract
{
public function getLocation()
{
$ipLite = new ip2location_lite;
$ipLite->setKey('APIKEY');
//Get errors and locations
$locations = $ipLite->getCity($_SERVER['REMOTE_ADDR']);
$errors = $ipLite->getError();
$country = strtolower($locations['countryName']);
return "$country";
}
}
以上代码将返回国家名称。如果用户从法国访问,我该如何重写 url 以使 url 变为 example.com/france/{controller}/{action}?
【问题讨论】:
-
您为什么认为该国家/地区表示语言偏好?应该使用 HTTP
Accept-Language标头。国家/地区代码对于特定国家/地区的内容很有用,例如货币和邮政编码表。 -
另外,最好不要为每种语言设置单独的目录。您应该使用 ccTLD 或以
Accept-Language指定的正确语言返回内容,同时保持相同的 URL 方案。
标签: php zend-framework