Best way to get hostname with php

前端 未结 6 2002
别跟我提以往
别跟我提以往 2020-12-05 03:51

I have a php application that is installed on several servers and all of our developers laptops. I need a fast and reliable way to get the server\'s hostname or some other

6条回答
  •  离开以前
    2020-12-05 04:22

    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')); 
    

提交回复
热议问题