3

I am trying to access an system variable within my Laravel 4 project. Similar to using ENV['VARIABLE_NAME'] to access a system variable in an RoR project.

Within my Laravel code getenv('VARIABLE_NAME') returns an empty string. However, I can access and print this variable to the screen using php -r "echo getenv('VARIABLE_NAME')" at the command prompt.

php -i confirmed that this variable is also stored in php's $_SERVER superglobal. However, attempting to access $_SERVER['VARIABLE_NAME'] from the database.php file of my project results in an Undefined index: VARIABLE_NAME error.

Can I not access arbitrary system variables from php for some reason (e.g., potential security issue, perhaps) ? If this is the case, how can I expose the system variable I need to my Laravel 4 project?

If configuration matters, I'm using php5-fpm and nginx to serve up my PHP on Ubuntu 13.04 Server. PHP version is 5.5.

The Alpha
  • 143,660
  • 29
  • 287
  • 307
Eli Hooten
  • 994
  • 2
  • 9
  • 23
  • 1
    I tried to access `$_SERVER["REDIRECT_URL"]` inside `app/config/database.php` and it worked just fine. May be you are trying to access `CLI` specific variable which may not be available for Web Request. – tharumax Nov 19 '13 at 00:42

2 Answers2

8

After fighting with this for much longer than necessary, the proper approach is to use the php5-fpm www.conf file in the pool.d directory of your php5-fpm install. For me this was at /etc/php5/fpm/pool.d/www.conf.

In www.conf there is a specific section of the file that lists several environment variables with the following syntax:

env[VARNAME] = $ENV_VAR_NAME

So just add your own and then you can then access these variables in your Laravel app with

getenv('VARNAME')

Works like a champ.

Eli Hooten
  • 994
  • 2
  • 9
  • 23
0

It looks like an nginx related problem, nginx passes parameters to PHP through fastcgi_param directives, so, you have to set it up, just add one you need where you set up other params, this is an example (Setting up FastCGI Variables)

; /etc/nginx/fastcgi_params
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param  PATH_INFO $fastcgi_script_name;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

Check How nginx processes a request and this answer (this one too) as well. I can access any environment variable from PHP on Apache. Hope this helps but can't be more specific.

Community
  • 1
  • 1
The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • I investigated using the `fastcgi_params` to accomplish my goals for quite awhile. I kept hitting roadblocks with this approach. See my accepted answer for what I finally ended up doing. Thanks for contributing, though. Your source links were really helpful for understanding how nginx interacts with fastCGI. – Eli Hooten Nov 20 '13 at 22:44