Reputation: 4045
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.
Upvotes: 19
Views: 17607
Reputation: 2561
You can try and use the global $_SERVER['SERVER_NAME']
This should tell you the host name of the system that's running the script. You could use this to determine what settings to use (depending on the system).
I don't know that this will output 'localhost' however and you may need to know your actual host name of your development machine.
Upvotes: 4
Reputation: 1889
you better put the cli running program outside www directory ,for example
c:\iis\www is the public html directory
the cli file should be put under c:\iis instead
Upvotes: 0
Reputation: 3138
if($_SERVER["REMOTE_ADDR"]=="127.0.0.1"){
$local = True;
}else{
$local = False;
}
EDIT
You could also check the first part of the address and see if the server is in the local network, the again assuming your server won't be in the local network when in production
Upvotes: 14
Reputation: 47321
On dev server
* * * * * php -r cronjob.php this_is_dev
On production server
* * * * * php -r cronjob.php this_is_live
In your script
switch ($argv[1])
case 'this_is_dev':
// load your dev configuration
break;
case 'this_is_live':
// load your live configuration
break;
default:
die('invalid server');
break;
}
since is meant for cronjob, the $argv
is exist
good luck with Windows :(
Upvotes: 1
Reputation: 22847
I know this word may sound strange to PHP developer, but have you considered build of your project?
In PHP there's nothing to compile, however changing copied files is one of features of any build process. You could specify 2 targets: production and dev. There would be no need for any conditionals, that should work, or may work, but under some circumstances won't.
Upvotes: 2
Reputation: 14946
I set an environment variable in the Apache configuration and check that. This has the advantage over using a PHP configuration file that all your application code remains exactly the same on PROD, TEST, DEV etc; no need to go and make changes after a check out, as the code just pulls the config from Apache.
To demonstrate, the following can be set in your VirtualHost configuration
SetEnv ENVIRONMENT PROD
In your PHP code, you can then check the environment with
$env = getenv('ENVIRONMENT');
If you feel the need to make this available everywhere, you can then use define
, but I don't find it necessary (I use the specified environment to load the appropriate configuration files and create a read-only Singleton configuration, which is then used to check any configuration options; better than if ($env == 'PROD') {}
type code, as all that logic is in the config, not in your code).
Upvotes: 6
Reputation: 3351
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.
Upvotes: 24
Reputation: 1267
i always make "config.php" wich i include to other php files...
it contains something like (home file):
$CONFIG['server']="dev";
$CONFIG['db_user']="root";
and thing like that... so at home i have this one up there and at server where site is running i have another one wich i dont update if not changes to it...
so on server i have something like:
$CONFIG['server']="prod";
$CONFIG['db_user']="lwu9918_admin";
and then in other php files:
include("config.php");
if($CONFIG['server']=="dev"){echo "Development";}
thing like that!
Upvotes: 1
Reputation: 34169
The server has no idea what environment it is unless you tell it to. What I do is use DEFINE to set the environment. My application code is the same on every instance but the my configuration files change. That way you can use .htaccess file include the configuration files on every script and check to see what they settings are.
Upvotes: 2
Reputation: 64409
Use a config file you include with a define in it
define("DEBUG",true); //set to false for live
and use that in your code, e.g.:
if(DEBUG){}
Upvotes: 5