Js determines the City Based on the ip address and jsip determines the city

Source: Internet
Author: User

Js determines the City Based on the ip address and jsip determines the city

<Script src = "http://counter.sina.com.cn/ip" type = "text/javascript" charset = gb2312> </script> <script> var sf = ILData [2]; if (sf. indexOf ("Beijing")> = 0) {window. location. href = "URL";} else if (sf. indexOf ("Hebei")> = 0) {window. location. href = "URL";} else if (sf. indexOf ("Tianjin")> = 0) {window. location. href = "URL"; // You can also define the content as text.} </script>

Php program js judges ip regions and outputs different content based on different ip addresses (Commercial Code)

We do not recommend using js for this processing. If you do this, the entire process is
1. Access the php page
2. The server outputs html
3. the browser runs js to check the ip Region
4. Submit the region information to the server in ajax Mode
5. The server returns the query result
6. js displays the query result on the current page.

Think about how long it will take. Therefore, it is best to directly determine the region on the php side, and then directly query and output it.
Provides a function written in php.

Function getCityByUserIp () {$ ip = false; if (isset ($ _ SERVER) {if (isset ($ _ SERVER ["HTTP_X_FORWARDED_FOR"]) {$ ip = $ _ SERVER ["HTTP_X_FORWARDED_FOR"];} else if (isset ($ _ SERVER ["HTTP_CLIENT_IP"]) {$ ip = $ _ SERVER ["HTTP_CLIENT_IP"];} else {$ ip = $ _ SERVER ["REMOTE_ADDR"];} else {if (getenv ("HTTP_X_FORWARDED_FOR") {$ ip = getenv ("HTTP_X_FORWARDED_FOR");} else if (getenv ("HTTP_CLIENT_IP") {$ ip = Getenv ("HTTP_CLIENT_IP");} else {$ ip = getenv ("REMOTE_ADDR") ;}} if (false ===$ ip) {return false ;} $ file = fopen ('int .dpool.sina.com.cn/...js&ip '. $ ip, 'R'); $ content = fread ($ file, 1024); fclose ($ file); if (! Preg_match ("/^ var remote_ip_info = (\\{ [^ \\}] + \\}); $/", $ content, $ _ match )) {return false;} $ ipInfo = json_decode ($ _ match [1]); return $ ipInfo ;}
The returned array includes country, province, city, etc. If false is returned, the ip address cannot be obtained or the ip address does not match in mainland China (Hong Kong, Macao and Taiwan seem to have pr ...... remaining full text>

JSP uses an IP address to determine the city code. That is to say, after obtaining the IP address, JSP outputs the specified city name based on the IP address.

What I want to say is that SP outputs the specified city name based on the IP address after obtaining the IP address .. This requires special data. Check the city name based on the IP address. I wonder if you have such a data source... If so, I have a blog post about obtaining real IP addresses.
Blog.163.com/..40743/

If not, I heard that there is a data webpage like this ..

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.