36

PHP 5.4 supports a built-in web server for development purposes. The app we are developing is configured via environment variables.

With Apache you'd do this:

SetEnv FAVORITE_COLOR white

With the normal CLI you can do this:

$ export FAVORITE_COLOR=black
$ php -a
php > echo $_SERVER['FAVORITE_COLOR'];

Is there a way to set these variables for the built-in web server?

hakre
  • 193,403
  • 52
  • 435
  • 836
Brad Koch
  • 19,267
  • 19
  • 110
  • 137
  • What if you set the environment variables like you did in the second example and then rather than `-a`, used the appropriate option to start the web server? – icktoofay Dec 09 '12 at 02:38
  • Nothing in `$_ENV`, typical stuff in `$_SERVER` (eg `'DOCUMENT_ROOT'`) – Brad Koch Dec 09 '12 at 02:41
  • Does [`getenv`](http://php.net/getenv) return anything? – Charles Dec 09 '12 at 03:30
  • That's... disturbing. If you use `setenv`, can you at least get back the same value in `getenv` or `$_ENV`? I'd test this myself, but I don't have a copy of 5.4 handy. – Charles Dec 09 '12 at 03:43
  • A variable set with `putenv("FAVORITE_COLOR=VIOLET_BLUE")` can be retrieved with `getenv("FAVORITE_COLOR")`, but `$_ENV` will stay empty. Disturbing, strong words =) – Brad Koch Dec 09 '12 at 03:51
  • what about safe_mode and safe_mode_allowed_env_vars in your php.ini? – didierc Dec 09 '12 at 04:51
  • safe_mode was [removed in PHP 5.4](http://php.net/manual/en/features.safe-mode.php), so no, it's not on. – Brad Koch Dec 09 '12 at 04:55
  • If you have the environment variable exported *before* you started the webserver, this should work. You probably need that per request? Can you give some example code that illustrates a bit more when/what you're trying to set? For the final page or for the routing script for example? – hakre Jan 09 '13 at 11:10
  • If there is really no way to set an environment variable for the builtin webserver except from the running PHP-process itself, you may consider to report this as http://bugs.php.net – KingCrunch Jan 09 '13 at 11:14

3 Answers3

54

Looks like E is excluded from variable_order setting running the built-in server. If you add the E to the variable_order setting, it works:

test.php

<?php
var_dump($_ENV['FOO']);

shell:

FOO=BAR php -d variables_order=EGPCS -S localhost:9090 /tmp/test.php

output:

string 'BAR' (length=3)

Tested on PHP 5.4.12

mcuadros
  • 4,098
  • 3
  • 37
  • 44
  • 1
    In case you are wondering why this does not work for setting container parameters in Symfony2 check out this issue: https://github.com/symfony/symfony/issues/10208 – Philipp Rieber Sep 25 '14 at 12:42
  • 4
    so there is no way to make it appear in `$_SERVER`? – caesarsol Nov 11 '15 at 18:34
  • 1
    @caesarsol It looks like a php issue reported in 2014 that was marked as won't fix in 2015. https://bugs.php.net/bug.php?id=67808 – Will B. May 19 '16 at 01:18
  • 1
    The `$_ENV` doesn't seem reliable, it could be disabled inside `php.ini` which we may not be able to change. I found that `getenv()` does work when`$_ENV` is empty on PHP 7 CLI web server. However at the same time, if these environment variables are set via Apache or NGINX, they appear as CGI variables, and it seems that Apache and NGINX populates the `$_SERVER` but not necessarily `getenv`. – CMCDragonkai Mar 02 '17 at 03:48
  • Does this work on windows? I tried `BASE_URL=http://localhost:3000 php -d variables_order=EGPCS -S localhost:3000` and got `'BASE_URL'` is not recognized as an internal or external command – lomse Aug 18 '17 at 16:58
  • 1
    @lomse it's working for me from the git-bash shell on windows, it would probably also work from the linux subsystem if you have that installed. – MSpreij Nov 27 '18 at 10:06
1

I use Window DOS to start the PHP server. I store my server startup commands in a text batch file (.bat) to save me from having to select and copy all of the commands at once and paste it into the DOS terminal (note the last blank line that I copy as well so the PHP server will automatically start when I paste the commands into DOS, otherwise I would need to manually use the Enter key to start the server).

Q:
cd Q:\GitLabRepos\myapps\test1
set APPLICATION_TITLE=My testing application with this first env variable
set SOME_OTHER_ENV_VAR2={"myJsonElement":"some value"}
E:\PHP8\php.exe -d variables_order=E -S localhost:8000 -c php.ini

The commands above explained:

The first line Q: changes to the drive where my code resides. The second line cd Q:\GitLabRepos\myapps\test1 changes directories to my root PHP application code (which is where I want to start the PHP server). Next I set some environment variables on lines 3 and 4. Then finally I start the PHP server with the -d variables_order=E parameter so I can use either $_ENV or getenv() to retrieve the environment variable values in my PHP code (eg. $_ENV['APPLICATION_TITLE'] or getenv('APPLICATION_TITLE')). If you exclude -d variables_order=E from the server startup command then you can only use getenv() to access the environment variables. I use the -c php.ini parameter to load additional PHP settings from a php.ini file but this can be excluded for simple server setup.

Then if I have a Q:\GitLabRepos\myapps\test1\index.php script with the following code:

<?php
echo getenv('APPLICATION_TITLE').'---'.$_ENV['APPLICATION_TITLE'].'...'.getenv('SOME_OTHER_ENV_VAR2');
?>

If I visit localhost:8000 in a web browser I should see

My testing application with this first env variable---My testing application with this first env variable...{"myJsonElement":"some value"}.

w. Patrick Gale
  • 1,643
  • 13
  • 22
-4

On Windows:

SET FOO=BAR
php -s localhost:9090
Fábio Paiva
  • 569
  • 4
  • 7