Identifying the server

Hi,

I have a development platform with a development server and a production server. I’ll upload my projects to both servers, but I want exceptions to be handled differently on development servers. Because I want to be able to upload the exact same code to both servers, I want php to detect which server it is running on and act accordingly. I thought this would be an easy task but it isn’t.

So far I tried the following:
Setting an environment variable in /etc/profile and reading it using php’s getenv function.
Didn’t work because from php you can only read a couple of environment variables, and not those in etc/profile.

Getting the server address and/or host using the $_SERVER superglobal.
Didn’t work for crons.

Setting a custom non-standard setting (error_message_mode) in php.ini.
Didn’t work because only standard settings will be available in ini_get.

Getting the ip address of the server using some linux function.
I frankly don’t know how to do this. Any ideas?

How can I do this in a simple way?

Regards,
Bas

By the way, I am now using display_startup_errors and ini_get in order to get the mode (default or live), because it is recommended to only turn on this flag in debug mode. However, I still like to know how to set custom server-specific flags in php.

I use auto_prepend_file in php.ini for this.

It includes a file that has (among others)


defined('DEVELOPENT_SERVER') or define('DEVELOPMENT_SERVER', true);

and in the index/bootstrap file of projects I have


defined('DEVELOPENT_SERVER') or define('DEVELOPMENT_SERVER', false);

Since you can also set this in the php.ini for CLI, it should also cover cron jobs.

It’s the easiest way I’ve been able to come up with so far.

Edit:

Forgot to mention: I have that auto_prepend_file on dev server(s) only, otherwise the approach doesn’t make sense :slight_smile:

Hi,

I thought of that approach. But I want to be able to upload identical code to both servers, and have the code itself figure out whether it is on a development server or live server. The define-approach is great normally, but it requires you to edit the project code every time you upload the file to a test or live server.