教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php函数获取在线ip与客户端ip

php函数获取在线ip与客户端ip

发布时间:2018-02-01   编辑:jiaochengji.com
分享一例获取在线ip地址与客户端ip的php代码,用php获取用户ip地址信息,有需要的朋友参考下。

例子,php代码获取ip地址。

代码:
 

复制代码 代码示例:

<?php
/**
* 获取客户端ip
* @return [string] [description]
*/
function getclientip() {
$ip = null;
if (isset($_server['http_x_forwarded_for'])) {
$arr = explode(',', $_server['http_x_forwarded_for']);
$pos = array_search('unknown',$arr);
if(false !== $pos) unset($arr[$pos]);
$ip = trim($arr[0]);
}elseif (isset($_server['http_client_ip'])) {
$ip = $_server['http_client_ip'];
}elseif (isset($_server['remote_addr'])) {
$ip = $_server['remote_addr'];
}
// ip地址合法验证
$ip = (false !== ip2long($ip)) ? $ip : '0.0.0.0';
return $ip;
}

/**
* 获取在线ip
* @return string
*/
function getonlineip($format=0) {
global $s_global;
if(empty($s_global['onlineip'])) {
if(getenv('http_client_ip') && strcasecmp(getenv('http_client_ip'), 'unknown')) {
$onlineip = getenv('http_client_ip');
} elseif(getenv('http_x_forwarded_for') && strcasecmp(getenv('http_x_forwarded_for'), 'unknown')) {
$onlineip = getenv('http_x_forwarded_for');
} elseif(getenv('remote_addr') && strcasecmp(getenv('remote_addr'), 'unknown')) {
$onlineip = getenv('remote_addr');
} elseif(isset($_server['remote_addr']) && $_server['remote_addr'] && strcasecmp($_server['remote_addr'], 'unknown')) {
$onlineip = $_server['remote_addr'];
}
preg_match("/[\d\.]{7,15}/", $onlineip, $onlineipmatches);
$s_global['onlineip'] = $onlineipmatches[0] ? $onlineipmatches[0] : 'unknown';
}

if($format) {
$ips = explode('.', $s_global['onlineip']);
for($i=0;$i<3;$i++) {
$ips[$i] = intval($ips[$i]);
}
return sprintf('%03d%03d%03d', $ips[0], $ips[1], $ips[2]);
} else {
return $s_global['onlineip'];
}
}

php获取远程客户端真实ip地址
php在内网机器获取公网IP的方法
php读取纯真ip数据库的简单例子
PHP获取本机的局域网IP地址方法
PHP获取局域网中计算机名、IP地址与MAC地址
PHP获取IP地址的多种方法
PHP通过IP获取地理位置的代码
PHP获取指定的IP网段信息
php IP获取城市API(纯真IP数据库)
php获取真实ip地址的实例分享
探讨:PHP获取域名及域名IP地址的方法
php通过IP获取地理位置的实例参考

您可能感兴趣的文章:
php函数获取在线ip与客户端ip
php获取远程客户端真实ip地址
深入解析PHP获取客户端IP的方法
php无法获取真实ip
PHP通过IP获取地理位置的代码
获取客户端的真实ip地址的php实现方法与原理
php获取客户端的真实IP的方法介绍
使用PHP来获取客户端和服务端IP
php 获取网站地址的函数代码
php获取客户端ip地址的代码

关键词: js获取客户端ip  PHP获取IP地址   
[关闭]
~ ~