[Php] obtains the IP address and the IP address of the remote data using the Sina api interface and php.

Source: Internet
Author: User
[Php] obtains the IP address by using the Sina api interface and php to obtain remote data, and obtains the corresponding IP address. this article and [Servlet] Javaweb use the Sina api interface, get the IP address and obtain the corresponding IP address location (click to open the link) as a companion article, but the backend programming language is changed to php.

To achieve the same effect, open the page, obtain the client ip address, and obtain the ip address location using the Sina api interface.


As you can see, IE6 is quite normal.

The directory structure is as follows, which is very simple. There are also two files. if you want to write a php file, you can also write it. but is it okay that the display layer and logic service layer are not mixed together?

The only difference is that the two files cannot be named ipget.php and ipget.html. Although the suffix names are different, the prefixes must not be the same,

Otherwise, IE6 may not be able to respond to the Json


The display layer page in ipGetView.html is exactly the same as that in [Servlet] Javaweb, which uses the Sina api interface to obtain the IP address and the corresponding IP address location (click to open the link, the Sina api interfaces are also identical, so we will not repeat them here.

 Javaweb-IP query

IP:

IP address attribution:

Script // creates Ajax objects. different browsers have different creation methods. In fact, this function is just a simple new statement. Function createXMLHttpRequest () {var XMLHttpRequest1; if (window. XMLHttpRequest) {XMLHttpRequest1 = new XMLHttpRequest ();} else if (window. activeXObject) {try {XMLHttpRequest1 = new ActiveXObject ("Msxml2.XMLHTTP");} catch (e) {XMLHttpRequest1 = new ActiveXObject ("Microsoft. XMLHTTP ") ;}}return XMLHttpRequest1;} function ajax () {var XMLHttpRequest1 = createXMLHttpRequest (); // specify the var url =" ipGetDo. p Hp "; XMLHttpRequest1.open (" POST ", url, true); // There is no way to explain it here. you can write all your JavaScript request headers in this way, XMLHttpRequest1.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded"); XMLHttpRequest1.send (null ); // XMLHttpRequest1.onreadystatechange = function () {// This 4 indicates if (XMLHttpRequest1.readyState = 4) {// 200 indicates that the returned result if (XMLHttpRequest1.status = 200) is correctly received {// json return result var data = eval ("(" + XMLHtt PRequest1.responseText + ")"); document. getElementById ("ip "). innerHTML = data. ip; document. getElementById ("area "). innerHTML = decodeURIComponent (data. area1) + decodeURIComponent (data. area2);} else {// if the result cannot be accepted normally, you must be disconnected or my server is disabled. Alert ("network connection interrupted! ") ;}};}Ajax (); script
Then, ipGetDo. in php, like [php] login system and output viewer information (click to open the link, use $ {_ SERVER ['remote _ ADDR ']} to obtain the user's ip address, and then use curl to remotely obtain data. Php uses curl to remotely obtain data in the following format:

  

For this reason, the ipgetdo.php code is as follows, and the data is obtained and printed directly, so that ipgetview.html can be obtained using Ajax.

  

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.