Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identify development vs. production server in PHP

Tags:

I work with two application servers on a daily basis: one development, one production. A variety of apps from different developers live on these boxes, as well as some scripts that run via cron. Currently, I am using the -D flag to httpd so that I can identify my production server in code, ie. isset($_SERVER['DEV']). Unfortunately, this does not work for scripts run from the command line since they're not under the Apache umbrella.

Essentially, I would like a clean, simple way to identify development vs. production that is available to every line of code.

What I have ruled out:

  • auto_prepend_file -- we are already using this directive in some applications, and you can't have more than one autoprepend.

What I am currently exploring:

  • Custom extension -- I'm sure creating a new extension that only defines a new constant (possibly influenced by an ini setting) would not be the hardest thing in the world, but I have no prior experience in this area.

So, got any tricks for identifying dev/prod that doesn't involve injecting code into every script or application?

like image 506
Annika Backstrom Avatar asked Sep 11 '09 18:09

Annika Backstrom


People also ask

What is the difference between development and production server?

Typically, a server in a development environment allows unrestricted access to and control by a user or group of users. A production server, on the other hand, is configured to restrict access to authorized users and to limit control to system administrators.

What is the difference between production server and non production server?

Production and non-production environments are logically and physically separated. The development, quality assurance, and production environments use separate equipment and environments, which are managed separately. Production data is not replicated or used in non-production environments.

How do I find my server environment?

In the Control Panel window, double-click System. In the System Properties dialog box, choose the Environment tab. Check to see if the value of system environment variable TZ is JST-9.

What are development servers?

A development server is a type of server that is designed to facilitate the development and testing of programs, websites, software or applications for software programmers. It provides a run-time environment, as well as all hardware/software utilities that are essential to program debugging and development.


2 Answers

use an environment variable

Just set an environment variable. It works on Windows and linux, they are even called the same thing now. Then just check $_ENV["DEVVSPROD"]

like image 124
DigitalRoss Avatar answered Feb 13 '23 13:02

DigitalRoss


I usually just do something like this:

if ($_SERVER['HTTP_HOST'] == 'localhost') // or any other host {      // development }  else {      // production } 
like image 34
Alix Axel Avatar answered Feb 13 '23 13:02

Alix Axel