How can we check if we are running the PHP script on localhost?

Yasen Zhelev picture Yasen Zhelev · Jan 24, 2011 · Viewed 13.7k times · Source

Is there a way to check if we are running a PHP script on localhost - development server or on live server - production server? Is there any PHP constant, variable, function,etc that can give me this information.

I need this to put different settings for production and development server. Now I parse the URL to see which one it is but I was wondering s there any better way to do that. My concern is that we may change the URL of the script and that may ruin my check.

I am looking few a solution with one config file and IF condition in it depending on which I will define different settings. The only problem is that I do not want to change the IF statement when there are changes on the server settings like hostname, document_root or something else that I am using to identify local/remote host.

And want to SVN update from one source without changing anything to my production server.

And I would like ideally to be able to run and CRON jobs with these settings.

Answer

Mikushi picture Mikushi · Jan 26, 2011

I use the SetEnv in my host definition to locate on which environment i am running (Dev, Stage, Production) :

<VirtualHost *:80>
(all the host info)
SetEnv SERVER_CONTEXT "dev"
</VirtualHost>

And each config file as an extra word in it : config.dev.ini, config.stage.ini, config.prod.ini, etc, ... It works like a charm.