PHP get IP address output as Array program code _ PHP Tutorial

Source: Internet
Author: User
Tags get ip
PHP obtains the IP address and outputs the code of the array program. There are many ways to get IP addresses in php. next I will introduce how to use linux commands to get IP addresses and convert them into array output, the following describes some common methods to obtain IP addresses from php, the following describes some common php IP address processing instances.

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:

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:

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:
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:
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;
}

Worker IP address processing instance...

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.