It mainly determines whether the client uses a proxy. Pay attention to the judgment order and first determine whether the client uses the proxy http_x_forwarded_for.
Function get_onlineip (){
$ 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 '];
}
Return $ onlineip;
}
Function real_ip_fun ()
{
Static $ realip = NULL;
If ($ realip! = NULL)
{
Return $ realip;
}
If (isset ($ _ server ))
{
If (isset ($ _ server ['HTTP _ x_forwarded_for '])
{
$ Arr = explode (',', $ _ server ['HTTP _ x_forwarded_for ']);
Foreach ($ arr as $ IP)
{
$ IP = trim ($ IP );
If ($ IP! = 'Unknown ')
{
$ Realip = $ IP;
Break;
}
}
}
Elseif (isset ($ _ server ['HTTP _ client_ip '])
{
$ Realip = $ _ server ['HTTP _ client_ip '];
}
Else
{
If (isset ($ _ server ['remote _ ADDR '])
{
$ Realip = $ _ server ['remote _ ADDR '];
}
Else
{
$ Realip = '0. 0.0.0 ';
}
}
}
Else
{
If (getenv ('HTTP _ x_forwarded_for '))
{
$ Realip = getenv ('HTTP _ x_forwarded_for ');
}
Elseif (getenv ('HTTP _ client_ip '))
{
$ Realip = getenv ('HTTP _ client_ip ');
}
Else
{
$ Realip = getenv ('remote _ ADDR ');
}
}
Preg_match ("/[\ D \.] {7, 15}/", $ realip, $ onlineip );
$ Realip =! Empty ($ onlineip [0])? $ Onlineip [0]: '0. 0.0.0 ';
Return $ realip;
}