2
votes

I am curious if doing something like setting your "environment" to Production, Dev, Staging, etc. based on a domain name such as dev.domain.com, staging.domain.com is a bad idea.

Can somebody just edit their hosts file to point dev.domain.com to domain.com so now the system thinks it's in Dev when it really is in Prod? This would mean any Dev specific code will be ran by a complete stranger.

Is this possible or is there a better way to determine your environment such as setting it manually in a server variable?

For reference, I am using PHP.

3
It really depends on how you have set up your subdomains, and the way your system detects what mode it is in. What people often do is password protect the dev. and staging. domains - Pekka
@Pekka I am asking about using this method to detect which mode I am in. Protecting the DEV and STAGING with passwords would work if you had to log in to the system to view anything, but if you had something like htpasswd on the DEV site but they are really getting served the PROD version (because they rerouted via hosts) then they wouldn't even get the password box, right? - cmfolio

3 Answers

3
votes

I would set the environment in a server variable on the machine. This allows you to control it during deployment and it cannot be mucked around with by people who are not authorized to do so. Although if you are worried about developers inadvertently connecting to production that is a whole other issue. You should be controlling access to production credentials via some other process such as jndi (in the java world) or environment keys replaced at runtime or deploy time (works in any language).

0
votes

Setting your "environment" to Production, Dev, Staging, etc. based on a domain name is absolutely safe. This is, because you have total control about what your domain name is.

BEWARE: do not trust what others make you believe what your domain name is. For example, $_SERVER['HTTP_HOST'] is not your domain name. It is the value of the HTTP Host header, that the client sent you, and is therefore free to manipulate.

0
votes

This is depends on how do you use your domain? Suppose that there are domain.com but there are several sub-domain, sub1.domain.com, sub2.domain.com, etc and every sub domain is managed by different user and/or application!