In CLI mode getenv('HOSTNAME')
returns HOSTNAME environment variable correctly, but when called in script returns FALSE
.
Why? How can I get the HOSTNAME variable in script?
The HOSTNAME is not available in the environment used by Apache, though it usually IS available in the environment used by the CLI.
For PHP >= 5.3.0 use this:
$hostname = gethostname();
For PHP < 5.3.0 but >= 4.2.0 use this:
$hostname = php_uname('n');
For PHP < 4.2.0 use this:
$hostname = getenv('HOSTNAME');
if(!$hostname) $hostname = trim(`hostname`);
if(!$hostname) $hostname = exec('echo $HOSTNAME');
if(!$hostname) $hostname = preg_replace('#^\w+\s+(\w+).*$#', '$1', exec('uname -a'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With