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.