简体   繁体   中英

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

When a script runs under Apache, I insert $_SERVER['SERVER_NAME'] value into an error reporting e-mail message.

However, if a Web script forks a "worker" job with nohup php ... , $_SERVER['SERVER_NAME'] appears to be empty there. Thus, if an error occurs, it's reported without a host name.

Can I reliably get the host name by means of PHP, without calling Unix hostname command?

php_uname("n")

(PHP 4 >= 4.0.2, PHP 5)
php_uname — Returns information about the operating system PHP is running on

php_uname() returns a description of the operating system PHP is running on. This is the same string you see at the very top of the phpinfo() output. For the name of just the operating system, consider using the PHP_OS constant, but keep in mind this constant will contain the operating system PHP was built on.

On some older UNIX platforms, it may not be able to determine the current OS information in which case it will revert to displaying the OS PHP was built on. This will only happen if your uname() library call either doesn't exist or doesn't work.

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 you can try one of these:

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

您可以使用_GLOBALS['MACHINENAME']直接从globals array获取信息。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM