简体   繁体   English

如何使用 PHP 或 JavaScript 根据地理位置或 IP 将访问者重定向到另一个站点?

[英]How can I redirect visitors to another site based in geographical location or IP using PHP or JavaScript?

I have a multilingual site with an 'index.html' sitting on example.com domain.我有一个多语言站点,其中包含一个位于 example.com 域的“index.html”。 This index.html should have some redirection code so when users go to 'example.com' they get redirected to either the spanish or English version of the site.这个 index.html 应该有一些重定向代码,所以当用户访问“example.com”时,他们会被重定向到站点的西班牙语或英语版本。

In its simplest form I'd like the conditional statement to read:我希望以最简单的形式阅读条件语句:

If IP is based in spanish speakers countries redirect to example.com/es else if anywhere else in the world redirect to example.com/en如果 IP 位于讲西班牙语的国家/地区,则重定向到 example.com/es 如果世界上其他任何地方都重定向到 example.com/en

How might I set this up using PHP or Javascript?我如何使用 PHP 或 Javascript 进行设置?

What you try is not a good idea because then you have to check the ip on every request and that can cause some useless timeout until the user is redirected to the site.您尝试的方法不是一个好主意,因为这样您就必须检查每个请求的 ip,这可能会导致一些无用的超时,直到用户被重定向到该站点。 And you need a service to check the URL which can cost.并且您需要一项服务来检查可能花费的 URL。

Normally you use the browser language to redirect.通常您使用浏览器语言进行重定向。 For this your browser sends a header with the language.为此,您的浏览器会发送带有该语言的标头。

$lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];

Then you can check the language and redirect to your site you want.然后您可以检查语言并重定向到您想要的站点。

switch($lang) {
    case "de-DE":
    case "fr-EN":
        header("Location: http://www.example.com/$lang/index.html");
        break;
    default:
        header("Location: http://www.example.com/en-GB/index.html");
        break;
}

You can try something like this.你可以尝试这样的事情。

If you want to use IP address to determinate user location, you should use special databases such as GeoIP2 , MaxMind , etc. Usually, they are not free, so you should dig this area to understand what you can use in this situation.如果你想使用 IP 地址来确定用户位置,你应该使用特殊的数据库,比如GeoIP2MaxMind等。 通常,它们不是免费的,所以你应该挖掘这个区域以了解在这种情况下你可以使用什么。

There's also another, less accurate but simple way: HTTP headers.还有另一种不太准确但简单的方法:HTTP 标头。 Modern browsers send specific set of user locales to show what language(s) visitor prefer.现代浏览器发送一组特定的用户区域设置来显示访问者喜欢的语言。

Keep in mind that browsers usually send list of accept locales (languages): ru-ru,ru;q=0.8,en-us;q=0.6,en;q=0.4 .请记住,浏览器通常会发送接受区域设置(语言)的列表: ru-ru,ru;q=0.8,en-us;q=0.6,en;q=0.4 Most of answers in the Internet ignore this issue and use only the first one option (or even only first two chars in the worst case).互联网上的大多数答案都忽略了这个问题,只使用第一个选项(或者在最坏的情况下甚至只使用前两个字符)。

For instance, you may notice that @Stony even suggested you use switch operator for the whole string which won't work in 99% cases because $_SERVER['HTTP_ACCEPT_LANGUAGE'] usually contains much more complex string than de-DE or fr-EN .例如,您可能会注意到 @Stony 甚至建议您对整个字符串使用switch运算符,这在 99% 的情况下不起作用,因为$_SERVER['HTTP_ACCEPT_LANGUAGE']通常包含比de-DEfr-EN复杂得多的字符串.

Stony has a great response to doing this via php, and I'd very much recommend that if that's your backend. Stony 对通过 php 执行此操作有很好的反应,如果这是您的后端,我非常建议您这样做。 But, you can also do this on the client with javascript several ways.但是,您也可以通过多种方式在客户端使用 javascript 执行此操作。

First is to get the user's coordinates via the browser using javascript.首先是使用javascript通过浏览器获取用户的坐标。 To do this, though, your users will have to allow you to view their location.但是,要做到这一点,您的用户必须允许您查看他们的位置。 You can do this pretty simply:你可以很简单地做到这一点:

let userLocationData = {};
if ("geolocation" in navigator) {
  navigator.geolocation.getCurrentPosition(function(position){
    userLocationData.lat = position.coords.latitude;
    userLocationData.long = position.coords.longitude;
  })
}

Of course, this is just the lat/long.当然,这只是纬度/经度。 You'll then need to use some api to get better data from that.然后,您需要使用一些 api 从中获取更好的数据。 I use GeoNames.我使用地理名称。 Now again, this depends on if you're doing this on the client or server, but I'll give you an example with Meteor's HTTP service, though you could do this with ajax or express or anything.再说一次,这取决于您是在客户端还是服务器上执行此操作,但我将举一个 Meteor 的 HTTP 服务示例,尽管您可以使用 ajax 或 express 或任何其他方法执行此操作。

let locationResults = HTTP.call('GET',`http://api.geonames.org/findNearbyPostalCodesJSON?formatted=true&lat=${userLocationData.lat}&lng=${userLocationData.long}&username=YOURUSERNAMEHERE`;

This will return an array of objects.这将返回一个对象数组。 So you could just do something like this:所以你可以做这样的事情:

userLocationData.country = locationResults[0].countryCode;

You can visit this url to see what that data might look like.您可以访问此 url 以查看该数据可能是什么样子。 http://api.geonames.org/findNearbyPostalCodesJSON?formatted=true&lat=40.987148&lng=-90.0803689&username=thatgibbyguy http://api.geonames.org/findNearbyPostalCodesJSON?formatted=true&lat=40.987148&lng=-90.0803689&username=thatgibbyguy

Now, you have their location and country.现在,您有了他们的位置和国家。 But can you tell language from that?但是你能从中分辨出语言吗? I'd say no.我会说不。 You should probably just check for their language:您可能应该检查他们的语言:

userLocationData.language = navigator.language;

That's going to use the user's own settings for language that you can use immediately.这将使用用户自己的语言设置,您可以立即使用。 From there you reroute pretty easily:从那里你很容易重新路由:

window.location.replace(`http://yoursite.com/${userLocationData.language}`/);

I finally did it making a few modifications to the Stony's code, my server didn't let me to use header because it was a free subdomain hosting site.我终于对 Stony 的代码做了一些修改,我的服务器不允许我使用标头,因为它是一个免费的子域托管站点。 So I changed by META instead... And it works I tested changing the language configuration of my browser, Check it out and thanks所以我改为通过 META 进行更改......并且它有效我测试了更改浏览器的语言配置,检查出来并感谢

    <?php

$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);



switch($lang) {
    case "es":
    echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=http://example_site.com\">";
        break;
    case "es-ES":
    echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=http://example_site.com\">";
        break;
    case "es-AR":
    echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=http://example_site.com\">";
        break;


    default:
        include("index.html");
        break;

}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用JavaScript计算网站访问者的数量? - How can I count the number of the visitors on my website using JavaScript? 如何根据IP位置将用户重定向到子域? - How to redirect users to a subdomain based on IP location? 在 WordPress 中,我如何按下按钮并将所有当前访问者重定向到另一个页面? - In WordPress, how can I push a button and redirect all my current visitors to another page? How Can i redirect to another PHP Page when i Confirm using Javascript without using PHP header Function - How Can i redirect to another PHP Page when i Confirm using Javascript without using PHP header Function Javascript 根据操作系统版本重定向网站访问者 - Javascript to redirect website visitors based on OS version 如何使用Java脚本或php计算网站当前访问者 - how to count the site current visitors using java script or php 如何使用JavaScript重定向到另一个页面? - How can i redirect to another page using javascript? 如何使用JavaScript将桌面用户重定向到一个站点并将移动用户重定向到另一个站点 - How can I redirect Desktop Users to one site and Mobile Users to another with JavaScript 如何使用JavaScript重定向到另一个PHP文件 - How to Redirect to another php file using javascript 如何过滤地理位置? - How do I filter on geographical location?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM