Re: Howto detect the hostname of the server?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 26 Nov 2011, at 00:24, Stuart Dallas wrote:
> On 26 Nov 2011, at 00:14, Andreas wrote:
>> Hi,
>> how could I identify the server the script runs on?
>> 
>> I've got a testserver on Windows and a remote system on Linux that need a couple of different settings like IP and port of db-server or folder to store logfiles.
>> 
>> I'd like to do something like:
>> 
>> if ( $_SERVER['some_key'] = 'my_test_box' ) {
>>   $db_host = '1.1.1.1';
>>   $db_port = 1234;
>> } else {
>>   $db_host = '2.2.2.2';
>>   $db_port = 4321;
>> }
>> 
>> 
>> I looked into phpinfo() but haven't found anything helpful, yet.
>> Have I overlooked something or is there another way to identify the server?
> 
> This should work on most Linux variants, so long as the hostname command exists and the PHP process has permission to execute it.
> 
> function getServerHostname($full = false)
> {
> 	$retval = `hostname`;
> 	if (!$full) {
> 		$retval = array_shift(explode('.', $retval));
> 	}
> 	return $retval;
> }


I should add that it would be better to put the server hostname into the environment in which PHP runs. That way it will be available via the $_ENV superglobal and fetching it will not cost a process execution. How you would do this depends on how you are running PHP.

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux