Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Linux Ask!

Apr 072009
 

Get user IP using PHP

Answer:

The following function is extracted from the popular phpMyAdmin, so why write it yourself?


function PMA_getIp()
{
    global $REMOTE_ADDR;
    global $HTTP_X_FORWARDED_FOR, $HTTP_X_FORWARDED, $HTTP_FORWARDED_FOR, $HTTP_FORWARDED;
    global $HTTP_VIA, $HTTP_X_COMING_FROM, $HTTP_COMING_FROM;

    // Get some server/environment variables values
    if (empty($REMOTE_ADDR) && PMA_getenv('REMOTE_ADDR')) {
        $REMOTE_ADDR = PMA_getenv('REMOTE_ADDR');
    }
    if (empty($HTTP_X_FORWARDED_FOR) && PMA_getenv('HTTP_X_FORWARDED_FOR')) {
        $HTTP_X_FORWARDED_FOR = PMA_getenv('HTTP_X_FORWARDED_FOR');
    }
    if (empty($HTTP_X_FORWARDED) && PMA_getenv('HTTP_X_FORWARDED')) {
        $HTTP_X_FORWARDED = PMA_getenv('HTTP_X_FORWARDED');
    }
    if (empty($HTTP_FORWARDED_FOR) && PMA_getenv('HTTP_FORWARDED_FOR')) {
        $HTTP_FORWARDED_FOR = PMA_getenv('HTTP_FORWARDED_FOR');
    }
    if (empty($HTTP_FORWARDED) && PMA_getenv('HTTP_FORWARDED')) {
        $HTTP_FORWARDED = PMA_getenv('HTTP_FORWARDED');
    }
    if (empty($HTTP_VIA) && PMA_getenv('HTTP_VIA')) {
        $HTTP_VIA = PMA_getenv('HTTP_VIA');
    }
    if (empty($HTTP_X_COMING_FROM) && PMA_getenv('HTTP_X_COMING_FROM')) {
        $HTTP_X_COMING_FROM = PMA_getenv('HTTP_X_COMING_FROM');
    }
    if (empty($HTTP_COMING_FROM) && PMA_getenv('HTTP_COMING_FROM')) {
        $HTTP_COMING_FROM = PMA_getenv('HTTP_COMING_FROM');
    }

    // Gets the default ip sent by the user
    if (!empty($REMOTE_ADDR)) {
        $direct_ip = $REMOTE_ADDR;
    }

    // Gets the proxy ip sent by the user
    $proxy_ip     = '';
    if (!empty($HTTP_X_FORWARDED_FOR)) {
        $proxy_ip = $HTTP_X_FORWARDED_FOR;
    } elseif (!empty($HTTP_X_FORWARDED)) {
        $proxy_ip = $HTTP_X_FORWARDED;
    } elseif (!empty($HTTP_FORWARDED_FOR)) {
        $proxy_ip = $HTTP_FORWARDED_FOR;
    } elseif (!empty($HTTP_FORWARDED)) {
        $proxy_ip = $HTTP_FORWARDED;
    } elseif (!empty($HTTP_VIA)) {
        $proxy_ip = $HTTP_VIA;
    } elseif (!empty($HTTP_X_COMING_FROM)) {
        $proxy_ip = $HTTP_X_COMING_FROM;
    } elseif (!empty($HTTP_COMING_FROM)) {
        $proxy_ip = $HTTP_COMING_FROM;
    } // end if... elseif...

    // Returns the true IP if it has been found, else false
    if (empty($proxy_ip)) {
        // True IP without proxy
        return $direct_ip;
    } else {
        $is_ip = preg_match('|^([0-9]{1,3}\.){3,3}[0-9]{1,3}|', $proxy_ip, $regs);
        if ($is_ip && (count($regs) > 0)) {
            // True IP behind a proxy
            return $regs[0];
        } else {
            // Can't define IP: there is a proxy but we don't have
            // information about the true IP
            return false;
        }
    } // end if... else...
} // end of the 'PMA_getIp()' function



Apr 022009
 

How to enable / disable service during startup in Fedora?

Answer:

Firstly, check if the service can be managed by chkconfig, e.g. Apache httpd

# chkconfig --list httpd

httpd           0:off   1:off   2:off   3:on    4:on    5:on    6:off

To disable the service during normal startup, use

# chkconfig httpd off

To re-enable it, use

# chkconfig httpd on --level 3,4,5

Mar 262009
 

How to replace multiple strings using sed?

Answer:

If you have multiple strings to replace, you can either using the "-e" option in sed, e.g.

sed -e 's/a/A/' -e 's/b/B/' < old.txt > new.txt

or, you can use a sed script file to store all the replace conditions

e.g. vi replace.sed

s/a/A/g
s/b/B/g
s/c/C/g

and execute like

sed -f replace.sed < old.txt > new.txt

Mar 142009
 

Install APC for PHP on Ubuntu

Answer:

APC is a very fast byte-code caching module for PHP. (It will become part of PHP6 in the future). By installing it, the performance of your PHP codes can be speed up by 200-300%.

To install it under Ubuntu,

# sudo apt-get install php-apc

Then restart Apache,

# sudo /etc/init.d/apache2 restart

See also: Compile/Install APC on RedHat/CentOS/Fedora Linux