本文目录一览:
利用php怎么获取用户ip
?php //方法1:
$ip = $_SERVER["REMOTE_ADDR"];
echo $ip;
//方法2:
$user_IP = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
$user_IP = ($user_IP) ? $user_IP : $_SERVER["REMOTE_ADDR"];
echo $user_IP;
//方法3:
function getRealIp()
{
$ip=false;
if(!empty($_SERVER["HTTP_CLIENT_IP"])){
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
for ($i = 0; $i count($ips); $i++) {
if (!eregi ("^(10│172.16│192.168).", $ips[$i])) {
$ip = $ips[$i];
break;
}
}
}
return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}
echo getRealIp();
//方法4:
if ($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"])
{
$ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
}
elseif ($HTTP_SERVER_VARS["HTTP_CLIENT_IP"])
{
$ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];
}
elseif ($HTTP_SERVER_VARS["REMOTE_ADDR"])
{
$ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];
}
elseif (getenv("HTTP_X_FORWARDED_FOR"))
{
$ip = getenv("HTTP_X_FORWARDED_FOR");
}
elseif (getenv("HTTP_CLIENT_IP"))
{
$ip = getenv("HTTP_CLIENT_IP");
}
elseif (getenv("REMOTE_ADDR"))
{
$ip = getenv("REMOTE_ADDR");
}
else
{
$ip = "Unknown";
}
echo $ip ;
//方法5:
if(getenv('HTTP_CLIENT_IP')) {
$onlineip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR')) {
$onlineip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR')) {
$onlineip = getenv('REMOTE_ADDR');
} else {
$onlineip = $HTTP_SERVER_VARS['REMOTE_ADDR'];
}
echo $onlineip;
PHP获取客户端IP
php获取客户端ip地址方法总结
用php获取客户端ip地址的方法,具体代码如下:
?php
$iipp=$_server["remote_addr"];
echo
$iipp;
?
//////////////////////////////////////////////
?php
$user_ip
=
($_server["http_via"])
?
$_server["http_x_forwarded_for"]
:
$_server["remote_addr"];
$user_ip
=
($user_ip)
?
$user_ip
:
$_server["remote_addr"];
echo
$user_ip;
?
///////////////////////////////////////////////
?php
function
get_real_ip(){
$ip=false;
if(!empty($_server["http_client_ip"])){
$ip
=
$_server["http_client_ip"];
}
if
(!empty($_server['http_x_forwarded_for']))
{
$ips
=
explode
(",
",
$_server['http_x_forwarded_for']);
if
($ip)
{
array_unshift($ips,
$ip);
$ip
=
false;
}
for
($i
=
0;
$i
count($ips);
$i++)
{
if
(!eregi
("^(10|172.16|192.168).",
$ips[$i]))
{
$ip
=
$ips[$i];
break;
}
}
}
return
($ip
?
$ip
:
$_server['remote_addr']);
}
echo
get_real_ip();
?
///////////////////////////////////////////////
php获取ip的算法:
?php
if
($http_server_vars["http_x_forwarded_for"])
{
$ip
=
$http_server_vars["http_x_forwarded_for"];
}
elseif
($http_server_vars["http_client_ip"])
{
$ip
=
$http_server_vars["http_client_ip"];
}
elseif
($http_server_vars["remote_addr"])
{
$ip
=
$http_server_vars["remote_addr"];
}
elseif
(getenv("http_x_forwarded_for"))
{
$ip
=
getenv("http_x_forwarded_for");
}
elseif
(getenv("http_client_ip"))
{
$ip
=
getenv("http_client_ip");
}
elseif
(getenv("remote_addr"))
{
$ip
=
getenv("remote_addr");
}
else
{
$ip
=
"unknown";
}
echo
"你的ip:".$ip
;
?
///////////////////////////////////////////////
?php
if(getenv('http_client_ip'))
{
$onlineip
=
getenv('http_client_ip');
}
elseif(getenv('http_x_forwarded_for'))
{
$onlineip
=
getenv('http_x_forwarded_for');
}
elseif(getenv('remote_addr'))
{
$onlineip
=
getenv('remote_addr');
}
else
{
$onlineip
=
$http_server_vars['remote_addr'];
}
echo
$onlineip;
?
如何使用php获取本机IP地址
function ip() {
if(getenv('HTTP_CLIENT_IP') strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
$ip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
$ip = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) $_SERVER['REMOTE_ADDR'] strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
$ip = $_SERVER['REMOTE_ADDR'];
}
return preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';
}
以上函数取自phpcms的获取ip方法
PHP如何获取用户的真实IP?
php获取用户真实ip的方法:
方法1:
//获取REMOTE_ADDR属性,直接可以得到ip
$ip = $_SERVER["REMOTE_ADDR"];
echo $ip;
方法2:
//从Server中获取HTTP_VIA,如果获取到就用HTTP_X_FORWARDED_FOR的值替换,如果获取不到就取REMOTE_ADDR的值。
$user_IP = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] :
$_SERVER["REMOTE_ADDR"];
$user_IP = ($user_IP) ? $user_IP : $_SERVER["REMOTE_ADDR"];
echo $user_IP;
方法3:
function getRealIp()
{
$ip=false; //初始化ip为false
if(!empty($_SERVER["HTTP_CLIENT_IP"])){ //如果HTTP_CLIENT_IP不为空
$ip = $_SERVER["HTTP_CLIENT_IP"]; //获取HTTP_CLIENT_IP的值
}
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { //如果HTTP_X_FORWARDED_FOR不为空
$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
//把HTTP_X_FORWARDED_FOR的值用,分割后存放数组ips
if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }//遍历处理
for ($i = 0; $i count($ips); $i++) {
if (!eregi ("^(10│172.16│192.168).", $ips[$i])) {
$ip = $ips[$i]; //获得真实ip
break;
}
}
}
return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}
echo getRealIp();