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