IP Detection

This little snippet tries to get the IP-Address of the user:

private function _detectIP(){
    //Mage::log('_detectIP', null, __CLASS__.'.log', true);
    if (getenv('HTTP_CLIENT_IP')) {
        $this->_ip  = getenv('HTTP_CLIENT_IP');
        Mage::log('Detected IP (getenv(\'HTTP_CLIENT_IP\')):', null, __CLASS__.'.log', true);
        Mage::log($this->_ip, null, __CLASS__.'.log', true);
    }
    elseif (getenv('HTTP_X_FORWARDED_FOR')) {
        $ips = explode(',', getenv('HTTP_X_FORWARDED_FOR')); 
        $this->_ip = (string)trim($ips[0]);
    }
    elseif (getenv('HTTP_X_FORWARDED')) {
        $ips = explode(',', getenv('HTTP_X_FORWARDED')); 
        $this->_ip = (string)trim($ips[0]);
    }
    elseif (getenv('HTTP_FORWARDED_FOR')) {
        $ips = explode(',', getenv('HTTP_FORWARDED_FOR')); 
        $this->_ip = (string)trim($ips[0]);
    }
    elseif (getenv('HTTP_FORWARDED')) {
        $ips = explode(',', getenv('HTTP_FORWARDED')); 
        $this->_ip = (string)trim($ips[0]);
    }
    else {
        $this->_ip = $_SERVER['REMOTE_ADDR'];
        Mage::log('Detected IP ($_SERVER["REMOTE_ADDR"]):', null, __CLASS__.'.log', true);
        Mage::log($this->_ip, null, __CLASS__.'.log', true);
    }
    Mage::log('No Detection', null, __CLASS__.'.log', true);
}

 

Leave a Reply

Your email address will not be published.

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.