Chhtprequest::getservername Not Always Working

Hello fellow Yii users :slight_smile:

I would like to configure my application so that it automatically picks up my dev.php or prod.php configuration file depending on which environment it’s ruuning in.

In order to do that I’m using CHttpRequest::getServerName to know which machine is running the code, however this function doesn’t work when I use it as part of a Command that I’ve written and which I run using yiic.

After some investigation I found that when running yiic $_SERVER doesn’t contain the HTTP_HOST or SERVER_NAME variables, would anyone know why ?

Also more generally speaking is what I’m trying to achieve a bad practice ? If yes what good practice is there to handle this situation ?

Many thanks in advance for your help.

Guyzeug.

You can use gethostname(). It probably is bad practise; a better way might be to have two different yiic files and use one in dev and the other on the live server. I started with your solution and have since switched to separate files.

Also, when you run a console command, it’s not an HTTP request, so you wouldn’t expect HTTP specific variables to be defined.

Thanks very much Keith for your quick reply, I’ll try to use gethostname() as a temporary solution, but might like you said to move to 2 different types of installation, that sounds better.

I do what you want to do and[size=2] I have the following "alternatives":[/size]




    $host= $_SERVER['HTTP_HOST'];

    $host= getenv('SERVER_NAME');

[size=2]    $host= gethostname();[/size]

    preg_match('/^localhost([:\/].*testpath.*)?$/',getenv('HTTP_HOST')



The third one is used when in CLI. Of the first two, I only need the second one. The last one is used to switch according to a local url.

Thanks :)