> is it well-written?

Well, not really... it is very redundant. A commonly accepted principle is the [**D**on't **R**epeat **Y**ourself. principle](https://deviq.com/don-t-repeat-yourself/). For example, the following block:

        if (getenv('HTTP_CLIENT_IP'))
            $mainIp = getenv('HTTP_CLIENT_IP');
        else if(getenv('HTTP_X_FORWARDED_FOR'))
            $mainIp = getenv('HTTP_X_FORWARDED_FOR');
        else if(getenv('HTTP_X_FORWARDED'))
            $mainIp = getenv('HTTP_X_FORWARDED');
        else if(getenv('HTTP_FORWARDED_FOR'))
            $mainIp = getenv('HTTP_FORWARDED_FOR');
        else if(getenv('HTTP_FORWARDED'))
            $mainIp = getenv('HTTP_FORWARDED');
        else if(getenv('REMOTE_ADDR'))
            $mainIp = getenv('REMOTE_ADDR');
        else
            $mainIp = 'UNKNOWN';
        return $mainIp;

Could be simplified by putting each key into an array:

    $ipKeys = ['HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', ...];

or that could be stored in a constant:

    const IP_KEYS = ['HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', ...];

Then iterate over those keys to set the value once a value is found:

    $mainIp = 'UNKNOWN';
    foreach ($ipKeys as $key) {
        if (getenv($key)) {
            $mainIp = getenv($key);
            break;
        }
    }
    return $mainIp;