cusejuice
cusejuice

Reputation: 10691

PHP Change Root path depending on localhost vs server

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.

Upvotes: 0

Views: 1433

Answers (3)

scrowler
scrowler

Reputation: 24435

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.

Upvotes: 4

Carlos Robles
Carlos Robles

Reputation: 10947

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

$isLocal=($_SERVER['REMOTE_ADDR']=="127.0.0.1" );

Upvotes: 0

Amal
Amal

Reputation: 76646

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.

Upvotes: 1

Related Questions