2
\$\begingroup\$

I am using below code to recognize which set up I need to use in Codeigniter:

$dev_urls = array('dev.com');
$prod_urls = array('prod.com');

if(in_array($_SERVER['HTTP_HOST'], $dev_urls)){
    define('ENVIRONMENT', 'development');
}
else if(in_array($_SERVER['HTTP_HOST'], $prod_urls)){
    define('ENVIRONMENT', 'production');
}
else{
    define('ENVIRONMENT', '');
}


if (defined('ENVIRONMENT'))
{
switch (ENVIRONMENT)
{
    case 'development':
        error_reporting(E_ALL);
        require('/application/config/development/dev_config.php');
    break;

    case 'testing':
    case 'production':
        error_reporting(0);
        require('/application/config/production/prod_config.php');
    break;

    default:
        exit('The application environment is not set correctly.');
}
}

I went through this answer, but, I am still skeptical about my code.

\$\endgroup\$
  • \$\begingroup\$ Can you voice your own skepticism? That make it easier for us to answer the question: 'Is it correct that I am still skeptical about my code?'. It would also be helpful if you could define the development and production urls. Currently you have two different domain, why not use that fact? There is $_SERVER['SERVER_NAME']. Or can all the different urls be on the same domain? In other words, what should this code be able to cope with? \$\endgroup\$ – KIKO Software Apr 16 '15 at 20:12
1
\$\begingroup\$

Switching behaviour based on the Host header is not something I would recommend. The environment should be a property of the server, rather than of the request. Depending on your hosting situation (whether you have an IP-based virtual host or name-based virtual host, and the details of how your webserver is configured), you might even be able to hit code on the production server using a request with a forged (or missing) Host header.

I recommend that you identify the production environment by some other means — probably by some configuration file that exists on the production server.

\$\endgroup\$
  • \$\begingroup\$ Thanks for your answer. So, I just need to put one extra file on production server. If the file is there then I need to consider production's configs. Am I right? And just quick clarification: can I use $_SERVER['SERVER_NAME'] to identify this? \$\endgroup\$ – Ronak Patel Apr 17 '15 at 17:27
  • \$\begingroup\$ Switching by $_SERVER['SERVER_NAME'] could work. Personally, I would prefer to put a configuration file on the production server that explicitly promotes it to that status. \$\endgroup\$ – 200_success Apr 17 '15 at 17:30
0
\$\begingroup\$

depending which CI Version you use - i would recommend to look for an environment constant added to the .htaccess file (or something similar for other Webservers). For more information click here

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.