PHP obtains the IP address and outputs the code as an array program.

Source: Internet
Author: User
Tags curl

PHP obtains the server IP address

Run ifconfig in PHP to get the Linux server IP address and output it as an array. The following code is used:

The code is as follows: Copy code

<? Php
Function getServerIp () {// use ifconfig to read the server IP address and output it as an array
$ Ss = exec ('/sbin/ifconfig | sed-n's/^ *. * addr: \ ([0-9.] \ {7 ,\}\). * $/\ 1/p', $ arr );
Return $ arr;
  }
$ Ips = getServerIp ();
 
Foreach ($ ips as $ k => $ v) {// filter IP addresses
If (substr ($ v, 127) = '000000' | substr ($ v,) = '10. '| substr ($ v, 192) =. 168 '| substr ($ v, 172) = '2017. 16 '){
Unset ($ ips [$ k]);
 }
}
Shuffle ($ ips); // sort again
Print_r ($ ips );
?>

Some php IP address acquisition examples

The code is as follows: Copy code

<? Php
Function GetIP (){
If (! Empty ($ _ SERVER ["HTTP_CLIENT_IP"]) {
$ Cip = $ _ SERVER ["HTTP_CLIENT_IP"];
}
Elseif (! Empty ($ _ SERVER ["HTTP_X_FORWARDED_FOR"]) {
$ Cip = $ _ SERVER ["HTTP_X_FORWARDED_FOR"];
}
Elseif (! Empty ($ _ SERVER ["REMOTE_ADDR"]) {
$ Cip = $ _ SERVER ["REMOTE_ADDR"];
}
Else {
$ Cip = "cannot be obtained! ";
}
Return $ cip;
}
Echo GetIP ();
?>

Here I have a public method to obtain the user IP address:

 

The code is as follows: Copy code
Function get_user_ip (){
If (isset ($ _ SERVER ['http _ CLIENT_IP ']) & $ _ SERVER ['http _ CLIENT_IP']! = 'Unknown '){
$ Ip = $ _ SERVER ['http _ CLIENT_IP '];
} Elseif (isset ($ _ SERVER ['http _ X_FORWARDED_FOR ']) & $ _ SERVER ['http _ X_FORWARDED_FOR']! = 'Unknown '){
$ Ip = $ _ SERVER ['http _ X_FORWARDED_FOR '];
} Else {
$ Ip = $ _ SERVER ['remote _ ADDR '];
        }
Return $ ip;
    }

PHP obtains the IP address region (call Sina API to obtain the IP address location)

 

The code is as follows: Copy code
Function get_location ($ ip ){
$ Curl = curl_init ();
Curl_setopt ($ curl, CURLOPT_URL, "http://int.dpool.sina.com.cn/iplookup/iplookup.php? Format = json & ip = ". $ ip );
$ Location = curl_exec ($ curl );
$ Location = json_decode ($ location );
If ($ location = FALSE) return "";
Return empty ($ location-> desc )? $ Location-> province. $ location-> city. $ location-> district. $ location-> isp: $ location-> desc;
}
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.