PHP implementation displays different language versions of websites in different countries

Source: Internet
Author: User

First of all, your site itself has multiple language versions. Otherwise you can only use JS to transform.

1. Through the IP to locate, this to refer to the third-party interface for the full return of data, but do not know whether my network is too slow or what the reason, personally think this method will Kaka:

get the IP of the visitor
functionGetIP () {if(isset($_server)) { if(isset($_server[' Http_x_forwarded_for '])) { $realip=$_server[' Http_x_forwarded_for ']; } ElseIf(isset($_server[' Http_client_ip '])) { $realip=$_server[' Http_client_ip ']; } Else { $realip=$_server[' REMOTE_ADDR ']; } } Else { if(getenv("Http_x_forwarded_for")) { $realip=getenv("Http_x_forwarded_for"); } ElseIf(getenv("Http_client_ip")) { $realip=getenv("Http_client_ip"); } Else { $realip=getenv("REMOTE_ADDR"); } } return $realip;}$ip=GetIP ();
Through the acquisition of IP and third-party interface to achieve docking, the return is the JSON format of data, conversion format, you can get a complete array, the following is taken country the value of this key to judge, can be country_id. Header to the URL of the language version you want to jump to. $res 0=file_get_contents("Http://ip.taobao.com/service/getIpInfo.php?ip= $ip");$res 0= Json_decode ($res 0,true);//echo $res 0[' data ' [' Country '];exit;if($res 0[' Data '] [' country '] = = ' America ' | |$res 0[' Data '] [' country '] = = ' UK ') { Header("location:/en");}Else if($res 0[' Data '] [' country '] = = ' Germany ') { Header("LOCATION:/DW");}Else{ Header("location:/");}

2. Determine which language to display by retrieving the meta-browser. This can be either with JS or with PHP can be achieved. I'm using PHP.

use PHP's $_server[' http_accept_language ' This function to get the meta value of the Web site currently open by the browser, and then cut the string to compare, then header to the specified website.

$langestrtolower(substr($_server[' http_accept_language '], 0, 2)); if ($lange= = ' en ') {header("location:/en");} Else if ($lange = = ' de ') {header("LOCATION:/DW");}

PHP implementation displays different language versions of websites in different countries

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.