Check LocalHost When Virtual Host Setting used?

How to test if code is running on localhost or not when VirtualHost settings are used on Apache? We used to test this and have config file configured according to that! Generally we need to have different setting on localhost to test Web Applications.

This code won't work on localhost when many different virtualHosts are used on localhost:

if($_SERVER['SERVER_NAME'] == 'localhost') {
    // LocalHost config
}

$_SERVER['SERVER_NAME'] would return VirtualHost name where code is running locally.
Example: vhost1, vhost2 and vhost3 are virtual hosts created on local system. Then $_SERVER['SERVER_NAME'] would return any one of the above vhost name.

In the case of VirtualHosts, following test works well:

if ($_SERVER["REMOTE_ADDR"] == '127.0.0.1') {
    // Localhost configuration
}

The above code is going to work from any virtual Host and programmer need not worry about the virtual Host (vhost1, vhost2 or vhost3) the code is in!

A new PHP method has come and that is gethostname(). The method is available after version 5.3 only. It can return machine name as 'satya-laptop'. This test can also work but it would fail when you share your code with colleague.