0

Using PHP, I would like to have a simple config.php file that enables me to check whether I am on localhost vs mydomain.com.

For example, on 'localhost':

$path = '/'

But when I upload to my server ('mydomain.com'), I'd like to have the path be:

$path = '/testing/

I'd like to be able to check whether I am developing locally vs when the site is uploaded to my ftp.

cusejuice
  • 10,285
  • 26
  • 90
  • 145

3 Answers3

4

You can get your current running server using $_SERVER['SERVER_NAME']:

$default_path = ($_SERVER['SERVER_NAME'] == 'localhost') ? '/' : '/testing/';

There are plenty of other variables in the $_SERVER superglobal array, if you want to use another one you should debug it and find whichever one you want (I'm on Amazon servers and I've got tons of Amazon specific variables to choose from too):

echo '<pre>'; print_r($_SERVER); exit;

SERVER_NAME is more reliable than others like HTTP_HOST, see this article for details.

Community
  • 1
  • 1
scrowler
  • 24,273
  • 9
  • 60
  • 92
1

You could probably check using $_SERVER['REMOTE_ADDR']. On localhost, this would return 127.0.0.1:

if ($_SERVER['REMOTE_ADDR'] === '127.0.0.1') {
    $path = '/';
} else {
    $path = '/testing/;
}

It is not 100% reliable, but works for most cases.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
0

I use to have this line in my config files for that purpose

$isLocal=($_SERVER['REMOTE_ADDR']=="127.0.0.1" );
Carlos Robles
  • 10,828
  • 3
  • 41
  • 60