10

Is it possible to check if the website (php) is running locally or on a hosted server? I want to enable some logs if the website is running locally and I don't want these to appear on the site online.. I can set a variable $local=1; but I'll have to change that before uploading.. is there anyway to automate this task?

Local Server : WampServer 2.0 / Apache WebServer: Apache

Nikhil Bhandari
  • 1,584
  • 2
  • 16
  • 34

7 Answers7

22

Check $_SERVER['REMOTE_ADDR']=='127.0.0.1'. This will only be true if running locally. Be aware that this means local to the server as well. So if you have any scripts running on the server which make requests to your PHP pages, they will satisfy this condition too.

Michael Mior
  • 28,107
  • 9
  • 89
  • 113
  • 1
    Thanks that worked! `$local = $_SERVER['REMOTE_ADDR']=='127.0.0.1' ? 1 : 0;` is what I used :) – Nikhil Bhandari Jul 08 '11 at 15:02
  • 1
    Note that if you've done any kind of work on your network `127.0.0.1` may not be your server's IP address. Be sure to replace the IP above with the actual IP of your server. – Charles Sprayberry Jul 08 '11 at 15:02
  • 5
    also allow for some servers (like Mac OSX) reporting the IPv6 value, so you would need: `$local = ($_SERVER['REMOTE_ADDR']=='127.0.0.1' || '::1')` or similar. You can also use the HTTP_HOST value if your local environment uses a different host name like mysite.local, etc. – ldg Jul 08 '11 at 15:04
  • @Matthieu localhost doesn't matter since we're talking IP addresses. Thanks for the comments Charles and ldg! – Michael Mior Jul 08 '11 at 15:13
  • @keppla's answer makes a lot more sense. – Rudisimo Jul 08 '11 at 15:13
  • This works only on a web environment, won't work on PHP CLI. – Pablo Pazos Oct 21 '21 at 02:26
  • 1
    @PabloPazos The question is explicitly asking about a web application. – Michael Mior Oct 21 '21 at 23:12
8

I believe the best approach is to 'fake' a testing mode, which can be done by creating a file in your local environment.
When I used this approach I created an empty text file called testing.txt and then used the following code:

if (file_exists('testing.txt')) {
    // then we are local or on a test environment
} else {
    // we are in production!
}

This approach is 100% compatible with any Operating System and you can use several test files in case you want a more granular approach (e.g. development.txt, testing.txt, staging.txt, or production.txt) in order to customise your deployment process.

JustCarty
  • 3,839
  • 5
  • 31
  • 51
lisawebs
  • 81
  • 1
  • 1
3

You should automate deployment

This is not directly the answer to your question, but in my opinion the better way. In an automated deployment process, setting a variable like $local = true, like other configuration values (for example your db-connection), would be no manual, error prone, task.

Checking for 'localness' is in my opinion the wrong way: you dont want to show your logs to every local visitor (a Proxy may be one), but only when deployed in a testing environment.

A popular tool for automated deployment is Capistrano, there should be PHP-Centric tools too.

keppla
  • 1,753
  • 2
  • 15
  • 29
  • If someone is visiting your site via the web, the IP address you see will never be `127.0.0.1` (or `::1` for IPV6), regardless of the usage of a proxy. (Unless of course you're running the proxy yourself on the same server ;) – Michael Mior Jul 08 '11 at 15:15
  • Jenkins and CruiseControl with PhpUnderControl are also good ones for continuous integration with PHP. – Rudisimo Jul 08 '11 at 15:16
  • @Michael Mior: yes, that was the case, proxy on the same machine. – keppla Jul 08 '11 at 16:56
2

Just in case this is useful to anybody, I made this function as the above answers didn't really do what I was looking for:

function is_local() {
    if($_SERVER['HTTP_HOST'] == 'localhost'
        || substr($_SERVER['HTTP_HOST'],0,3) == '10.'
        || substr($_SERVER['HTTP_HOST'],0,7) == '192.168') return true;
    return false;
}
Ben
  • 4,707
  • 5
  • 34
  • 55
1
$whitelist = array(
    '127.0.0.1',
    '::1'
);

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

I have build this function that checks if current server name has name server records, normally local server don't has.

<?php
function isLocal ()
{
  return !checkdnsrr($_SERVER['SERVER_NAME'], 'NS');
}
?>
xudre
  • 2,731
  • 2
  • 19
  • 19
0

Your remote server is unlikely to have a C drive! So I run with this:

//Local detection
$root = $_SERVER["DOCUMENT_ROOT"];
$parts = explode("/",$root);
$base = $parts[0];
$local = false;
if ($base == "C:") { 
 $local = true; //Change later for if local
}
mayersdesign
  • 5,062
  • 4
  • 35
  • 47