Home > Backend Development > PHP Tutorial > PHP obtains IP address and outputs it as array program code_PHP tutorial

PHP obtains IP address and outputs it as array program code_PHP tutorial

WBOY
Release: 2016-07-13 10:48:02
Original
1013 people have browsed it

There are many ways to obtain the IP address in PHP. Below I will introduce the use of Linux commands to obtain the IP address and then convert it into an array for output. The following also introduces some common examples of PHP IP address processing.

PHP Get Server IP Address

Use PHP to execute ifconfig to obtain the Linux server IP and output it as an array. The following is the code:

The code is as follows Copy code
 代码如下 复制代码

function getServerIp(){ //用ifconfig读取服务器IP并输出为数组
$ss = exec('/sbin/ifconfig | sed -n 's/^ *.*addr:([0-9.]{7,}) .*$/1/p'',$arr);
return $arr;
}
$ips=getServerIp();

foreach($ips as $k=>$v){//过滤IP
 if(substr($v,0,3)=='127' || substr($v,0,3)=='10.' || substr($v,0,7)=='192.168' || substr($v,0,6)=='172.16'){
  unset($ips[$k]);
 }
}
shuffle($ips);//重新排序
print_r($ips);
?>

function getServerIp(){ //Use ifconfig to read the server IP and output it as an array
$ss = exec('/sbin/ifconfig | sed -n 's/^ *.*addr:([0-9.]{7,}) .*$/1/p'',$arr);< br /> return $arr;
}
$ips=getServerIp();

foreach($ips as $k=>$v){//Filter IP
if(substr($v,0,3)=='127' || substr($v,0,3)=='10.' || substr($v,0,7)=='192.168' | | substr($v,0,6)=='172.16'){
unset($ips[$k]);
}
}
shuffle($ips);//Reorder
print_r($ips);
?>

Some examples of obtaining IP address with 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 = "无法获取!";
}
return $cip;
}
echo GetIP();
?>

The code is as follows Copy code

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 = "Unable to obtain!";
}
return $cip;
}
echo GetIP();
?>

I have a relatively common method to obtain the user’s IP address:

 代码如下 复制代码
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 gets the IP location (call Sina API to get the IP location)

 代码如下 复制代码
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;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632798.htmlTechArticleThere are many ways to obtain the IP address in php. Below I will introduce the use of linux commands to obtain the IP address and then convert it to Array output, some commonly used examples of php ip address processing are also introduced below...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template