问题描述:

I need to check if my server is on the live or development mode. Currently I am doing it like this:

<cfset isLive = FindNoCase("www.mydomain.com",CGI.SERVER_NAME) NEQ 0>

But the above does not seem foolproof. If the user tries

  • http://mydomain.com or
  • https:///mydomian.com or
  • http://www.mydomain.com

.. it will not work properly. Can anyone tell me how to accomplish this with a regex?

网友答案:

Turn it around and check for the dev mode instead of live. That will give you less options to struggle with. This solution is not foolproof either.

I would recommend:

  1. Define the environment in a settings file. Make it accessible to your application.
  2. Set your version management system to ignore it on commits and updates.
  3. Read the environment settings from the settings file and use it in the application.
网友答案:
 <cfset isLive = ReFindNoCase("^(www\.)?mydomain\.com$",CGI.SERVER_NAME) NEQ 0>

The good news is that CGI.SERVER_NAME only includes the domain name used to access the site - not the protocol information (even on HTTPS), so you don't have to worry about that.

I'm guessing from your examples that if the site is accessed using "www" prefix or no prefix that it should be the live site and all other prefixes - or any other domain name - will indicate the dev site.

This would mean that if you ever need another domain prefix on the live site for any reason that this would return false.

Here is how to change it if you needed the "bob" prefix on the live site:

 <cfset isLive = ReFindNoCase("^(((www)|(bob))\.)?mydomain\.com$",CGI.SERVER_NAME) NEQ 0>

You could also go the other way and look for something like "test" or "dev" anywhere in the domain name to establish that it is a test site:

 <cfset isTest = ReFindNoCase("\b((test)|(dev))\b",CGI.SERVER_NAME) NEQ 0>
相关阅读:
Top