How can I detect if the user is on localhost in PHP?

Richie Marquez picture Richie Marquez · Jan 13, 2010 · Viewed 88.4k times · Source

In other words, how can I tell if the person using my web application is on the server it resides on? If I remember correctly, PHPMyAdmin does something like this for security reasons.

Answer

mauris picture mauris · Jan 13, 2010

You can also use $_SERVER['REMOTE_ADDR'] for which IP address of the client requesting is given by the web server.

$whitelist = array(
    '127.0.0.1',
    '::1'
);

if(!in_array($_SERVER['REMOTE_ADDR'], $whitelist)){
    // not valid
}