2024年10月18日 建站教程
功能介绍:利用用户的IP地址和所在城市从而实现一个城市分站功能,这里我们会用到一些插件,比如:WP GeoIP,MaxMind GeoLite2 City 或 IP2Location等等!
具体代码如下:
<?php
function redirect_by_location() {
// 获取用户的 IP 地址
$user_ip = $_SERVER['REMOTE_ADDR'];
// 使用插件获取用户所在城市
$user_city = geoip_detect2_get_city_name_by_ip($user_ip);
if ($user_city == 'beijing') {
header('Location: https://lmcjl.com/vuejs/');
exit();
}
elseif ($user_city == 'shanghai') {
header('Location: https://lmcjl.com/js');
exit();
}
else {
header('Location: https://lmcjl.com/');
exit();
}
}
add_action('init', 'redirect_by_location');
?>
本文链接:http://so.lmcjl.com/news/15659/