Click Here
Home Blog Gallery Links Tutorials Projects

PHP

Some php programming tricks

Detect User's IP

<?php  
    
if(!function_exists("gethost")){
        function 
gethost($ip) {
            
$list=explode(","$ip);
            
$ret="";
            foreach(
$list as $item) {
                
$item=trim($item);
                
$tmp=@gethostbyaddr($item);
                if(
$tmp==$item$tmp="";
                
$ret.=$tmp." ";
            }
            
$ret=trim($ret);
            return 
addslashes($ret);
        }
    }

    function 
isValidIP($ip){
        if(
filter_var($ipFILTER_VALIDATE_IPFILTER_FLAG_IPV4) || 
        
filter_var($ipFILTER_VALIDATE_IPFILTER_FLAG_IPV6)) return true;
        else return 
false;
    }

    function 
getFirstIP($ip,$delim){
        if (
strstr($ip$delim)) {
            
$ips explode($delim$ip);
            foreach(
$ips as $ip) {
                if (
isValidIP($ip)) return $ip;
            }
            return 
$ips;
        } else return 
$ip;
    }
    
$ips = array();
    
$proxies = array();
    
$logmsg="";
    
    if (!empty(
$_SERVER["HTTP_X_FORWARDED_FOR"])) {
        if (
isValidIP($_SERVER["HTTP_X_FORWARDED_FOR"])) {
            if (!
in_array($_SERVER["HTTP_X_FORWARDED_FOR"],$proxies)) $proxies[] = $_SERVER["HTTP_X_FORWARDED_FOR"];
        } else {
            
$logmsg.= "Proxy (HTTP_X_FORWARDED_FOR) not valid: ".$_SERVER["HTTP_X_FORWARDED_FOR"]."\n";
        }
    } 
    
    if (!empty(
$_SERVER["HTTP_VIA"])) {
        
$via_ignorelist = array(
            
"1.1 squid.test (squid/3.5.20)"
        
);
        if (
isValidIP($_SERVER["HTTP_VIA"])) {
            if (!
in_array($_SERVER["HTTP_VIA"],$proxies)) $proxies[] = $_SERVER['HTTP_VIA'];
        } else {
            if (!
in_array($_SERVER["HTTP_VIA"],$via_ignorelist)) $logmsg.= "Proxy (HTTP_VIA) not valid: ".$_SERVER["HTTP_VIA"]."\n";
        }
    } 
    if (!empty(
$_SERVER["REMOTE_ADDR"])) {
        if (
isValidIP($_SERVER["REMOTE_ADDR"])) {
            if (!
in_array($_SERVER["REMOTE_ADDR"],$ips)) $ips[] = $_SERVER["REMOTE_ADDR"];
        } else {
            
$logmsg.= "IP (REMOTE_ADDR) not valid: ".$_SERVER["REMOTE_ADDR"]."\n";
        }
    } 
    if (!empty(
$_SERVER["HTTP_CLIENT_IP"]) && isValidIP($_SERVER["HTTP_CLIENT_IP"])) {
        if (
isValidIP($_SERVER["HTTP_CLIENT_IP"])) {
            if (!
in_array($_SERVER["HTTP_CLIENT_IP"],$ips)) $ips[] = $_SERVER["HTTP_CLIENT_IP"];
        } else {
            
$logmsg.= "IP (HTTP_CLIENT_IP) not valid: ".$_SERVER["HTTP_CLIENT_IP"]."\n";
        }
    } 
    
    if (!empty(
$ips)) $ip $ips[0]; 
    else 
$ip "unknown";
    if (!empty(
$proxies)) $proxy $proxies[0];
    else 
$proxy "";
    if (
count($ips) > 1$logmsg .= "Multiple ip's: ".implode(" "$ips)."\n";
    if (
count($proxies) > 1$logmsg .= "Multiple proxies: ".implode(" "$proxies)."\n";
    
    if(
$ip != "unknown") {
        if (
isValidIP($ip)) $host=gethost($ip);
        else 
$host="[Invalid IP]";
    } else 
$host "";
    
    if (
$logmsg) {
        
$file=fopen("$prefix/logs/getip.txt""a");
        
$msg getinfo()."\n".$logmsg;
        
fwrite($file$msg);
        
fclose($file);
    }
    
    
/*if (!isValidIP($ip)) {
        $ret=getFirstIP($ip,',');
        if (gettype($ret) == "array") {
            foreach ($ret as $ip){
                $ret2=getFirstIP($ip,':');
                if (gettype($ret2) == "array"){
                    $ip=false; 
                } else $ip = $ret2;    
            }
        } else $ip = $ret;
        if (!isValidIP($ip)) {
            $ret=getFirstIP($ip,':');
            if (gettype($ret) == "array"){
            $ip=false; 
            } else $ip = $ret;
        }
    }*/


?>

Add a Comment

Get Your IP Instantly
Name:
Enter the text from the image below to the text field next to it (text is case sensitive):
Email:

Name:
Password:

Enter Captcha (Case Sensitive):


Register
English
Lietuviškai

Unique visit count:
484 visits on page.
5353 visits on site.
Detailed Statistics
Misceleanous Content