Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP getenv('HOSTNAME')

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?

like image 522
Dziamid Avatar asked Dec 13 '22 13:12

Dziamid


1 Answers

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')); 
like image 123
ghbarratt Avatar answered Jan 06 '23 01:01

ghbarratt