Is there a function or PHP variable indicating the local host name?

When the script runs under Apache, I insert the value $_SERVER['SERVER_NAME']in the error message sending message.

However, if the web script creates a workstation with nohup php ..., $_SERVER['SERVER_NAME']it turns out to be empty there. Thus, if an error occurred, it reported without a host name.

Can I reliably get the hostname using PHP without calling the Unix command hostname?

+5
source share
3 answers

php_uname ("n")

(PHP 4> = 4.0.2, PHP 5)
php_uname - returns information about the PHP operating system running on

php_uname() PHP . , phpinfo(). , PHP_OS, , PHP.

UNIX , PHP. , uname() , .

+14

PHP >= 5.3.0 :

$hostname = gethostname();

PHP < 5.3.0, >= 4.2.0 :

$hostname = php_uname('n');

PHP < 4.2.0 :

$hostname = getenv('HOSTNAME'); 
$hostname = trim(`hostname`); 
$hostname = preg_replace('#^\w+\s+(\w+).*$#', '$1', exec('uname -a')); 
+1

_GLOBALS['MACHINENAME'] globals array.

0

All Articles