Friday, January 27, 2017

Error code from API call from one domain but not other Undefined index:

Leave a Comment

Sometimes my domain (example.test.org) is showing me an error, when making any API call.

"{"error":{"code":500,"message":"Undefined index: DB_HOST","file":"\/var\/www\/app\/config\/production\/database.php","line":7}}". 

But with the public IP of my local machine it's working. Any reason it would return an error from one domain but not another? The API is running on Laravel 4.2.

The output of database.php is

<?php  return array(   'default' => 'pgsql',   'connections' => array(     'pgsql' => array(       'host'     => $_ENV['DB_HOST'],       'port'     => $_ENV['DB_PORT'],       'database' => $_ENV['DB_NAME'],       'username' => $_ENV['DB_USER'],       'password' => $_ENV['DB_PASS'],     ),   ), ); 

These values come from /var/www/.env.php which looks like

return array(   'DB_HOST'    => 'my-app.cvrrctfasmvk.us-east-1.rds.amazonaws.com',   'DB_PORT'    => '*****',   'DB_NAME'    => '**************',   'DB_USER'    => '**********',   'DB_PASS'    => '***********',   'SMTP_HOST'  => '*******************',   'SMTP_USER'  => '***********************',   'SMTP_PASS'  => '********************************',   'AWS_KEY'    => '****************************',   'AWS_SECRET' => '*******************',   'AWS_QUEUE'  => '*****************************************',   'FB_APP_ID'  => '*****************',   'FB_APP_SECRET' => '*********************'  ); 

DB Host file looks like this by the way. with of course the identifiable values being changed to x

<?php  return array(   'DB_HOST'    => 'my-app-.xxxxxx.us-east-1.xxx.amazonaws.com',   'DB_PORT'    => 'xxxx',   'DB_NAME'    => 'xxxx_app_xxx_db',   'DB_USER'    => 'xxxx',   'DB_PASS'    => 'xxxx',   'SMTP_HOST'  => 'email-xxx.xxxx.amazonaws.com',   'SMTP_USER'  => 'xxxxxxx',   'SMTP_PASS'  => 'xxxx',   'AWS_KEY'    => 'xxx',   'AWS_SECRET' => 'xxxx',   'AWS_QUEUE'  => 'https://sqs.xxxxx.amazonaws.com/xxxx',   'FB_APP_ID'  => 'xxxxx',   'FB_APP_SECRET' => 'xxxx'  ); 

It looks like it is having trouble reading the /var/www/.env.php file. As the first item in the array is returning an error.

3 Answers

Answers 1

It seems there is a problem in your .env file that creates this error. Sometimes .env file is not processed as a PHP file because you may use a short opening tag in the .env.php file. Using a normal opening tag may solve it. Also check to find, is there any character in the values (especially the ones you hid it with x) which may break the array in your .env.php file.

Answers 2

Your best shot is to check your variables_order string at you php.ini file, it does control the order of the super globals loading and which variables you want to have, most probably you're missing an E in the variables_order string.

From the PHP man pages :

variables_order string Sets the order of the EGPCS (Environment, Get, Post, Cookie, and Server) variable parsing. For example, if variables_order is set to "SP" then PHP will create the superglobals $_SERVER and $_POST, but not create $_ENV, $_GET, and $_COOKIE. Setting to "" means no superglobals will be set.

Read more here PHP man pages

I think you're loading $_ENV only locally, since you probably have different php.ini files.

Answers 3

Clearly, when you get this error message, it's because your $_ENV variable doesn't have a 'DB_HOST' index.

Sometimes environment variables are not present in the superglobal $_ENV but can be fetched with getenv()

I've just tested this on my webserver (running PHP 5.5.9) :

<?php // test.php echo $_ENV['PATH']; ?> 

then

$ php test.php PHP Notice:  Undefined index: PATH in /tmp/env.php on line 2 

while this works :

<?php // test.php echo getenv("PATH"); ?>  $ php test.php /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment