1

According to the documentation on getenv, I should be able to use it to check the $_SERVER variable.

<?php
// Example use of getenv()
$ip = getenv('REMOTE_ADDR');

// Or simply use a Superglobal ($_SERVER or $_ENV)
$ip = $_SERVER['REMOTE_ADDR'];

However, this only seems to work for me when run from a web server (Apache), but not from the command line. The $_SERVER variable is populated correctly in both cases, but getenv only reads it on the web server.

<?php
var_dump(  getenv('SCRIPT_FILENAME'));
var_dump($_SERVER['SCRIPT_FILENAME']);

Output on Apache to browser:

/var/www/localhost/questions-answers/scratch.php:7:
string '/var/www/localhost/questions-answers/scratch.php' (length=48)
/var/www/localhost/questions-answers/scratch.php:8:
string '/var/www/localhost/questions-answers/scratch.php' (length=48)

Output on CLI:

/var/www/localhost/questions-answers/scratch.php:7:
bool(false)
/var/www/localhost/questions-answers/scratch.php:8:
string(11) "scratch.php"

I feel like there's some missing option to set in my php.ini but I can't find any reference for that suspicion. This is the inverse of this question.

Community
  • 1
  • 1
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
  • There is a difference between an environment variable and php's superglobal variables. What makes you certain that `$_SERVER` should be an environment variable in CLI usage? – arkascha Jan 27 '17 at 20:24
  • @arkascha the example on http://php.net/manual/en/function.getenv.php but I see what you're saying. Thanks, I didn't realize it wasn't possible, especially considering the comments on that page referring to it working in CLI. – Jeff Puckett Jan 27 '17 at 20:25
  • I don't see that the example says that it should be an environment variable... I suggest you simply compare `$_SERVER` and `$_ENV` to get an overview... – arkascha Jan 27 '17 at 20:27
  • @arkascha I don't understand what you mean. Do you see the example `$ip = getenv('REMOTE_ADDR'); // Or simply use a Superglobal ($_SERVER or $_ENV) $ip = $_SERVER['REMOTE_ADDR'];` – Jeff Puckett Jan 27 '17 at 20:29
  • especially with all the comments about its case-insensitivity. – Jeff Puckett Jan 27 '17 at 20:32
  • Sure, but where does it say that the sets of superglobals and environment variables are identical in all situations? – arkascha Jan 27 '17 at 20:56

0 Answers0