Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

amazon-ec2

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.

like image 743
Alex Borsody Avatar asked Jan 18 '17 21:01

Alex Borsody


Video Answer


2 Answers

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.

like image 181
mrbm Avatar answered Oct 23 '22 05:10

mrbm


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
like image 40
Ki Jéy Avatar answered Oct 23 '22 06:10

Ki Jéy