[PHP] Howto detect the hostname of the server?
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? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Howto detect the hostname of the server?
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; } -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
Re: [PHP] Howto detect the hostname of the server?
Hi. On Saturday 26 Nov 2011 at 00:14 Andreas wrote: how could I identify the server the script runs on? [snip] I looked into phpinfo() but haven't found anything helpful, yet. Have I overlooked something or is there another way to identify the server? Wouldn't the server IP address in $_SERVER['SERVER_ADDR'] or the hostname in $_SERVER['SERVER_NAME'] do the trick? That's what I use. The second one is handy for differentiating between sites when using Apache name-based virtual hosts on the same IP. Full list here: http://uk.php.net/manual/en/reserved.variables.server.php Cheers, Mark -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Howto detect the hostname of the server?
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
Re: [PHP] Howto detect the hostname of the server?
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) { This line should have a trim... $retval = trim(`hostname`); Without that, the full hostname will have a new line on the end which is less than ideal. if (!$full) { $retval = array_shift(explode('.', $retval)); } return $retval; } -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
Re: [PHP] Howto detect the hostname of the server?
On 26/11/2011, at 1:14 PM, 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? php_uname('n'); http://php.net/php_uname --- Simon Welsh Admin of http://simon.geek.nz/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Howto detect the hostname of the server?
Am 26.11.2011 01:35, schrieb Simon J Welsh: On 26/11/2011, at 1:14 PM, Andreas wrote: how could I identify the server the script runs on? php_uname('n'); http://php.net/php_uname Great, that even works on a ssh-tunnel. I got derailed by the fact that my tunnel maps the remote server to localhost:80 so $_SERVER['SERVER_ADDR'] and $_SERVER['SERVER_NAME'] wouldn't help. On the other hand for everyone on the remote LAN $_SERVER['...'] would work ok, though I didn't realise that before. Thanks :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Howto detect the hostname of the server?
On 11/25/2011 7:35 PM, Simon J Welsh wrote: On 26/11/2011, at 1:14 PM, 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? php_uname('n'); http://php.net/php_uname folks try: $_SERVER['SERVER_NAME'] Cheers, Curtis
Re: [PHP] Howto detect the hostname of the server?
Andreas maps...@gmx.net wrote: Am 26.11.2011 01:35, schrieb Simon J Welsh: On 26/11/2011, at 1:14 PM, Andreas wrote: how could I identify the server the script runs on? php_uname('n'); http://php.net/php_uname Great, that even works on a ssh-tunnel. I got derailed by the fact that my tunnel maps the remote server to localhost:80 so $_SERVER['SERVER_ADDR'] and $_SERVER['SERVER_NAME'] wouldn't help. On the other hand for everyone on the remote LAN $_SERVER['...'] would work ok, though I didn't realise that before. This may work in some circumstances, but not others. For example, I have a VPS with several sites hosted on it. php_uname will always return the default host name, which very likely might be different than the virtual host's hostname. I'm not doing any ssh tunnelling in my local setups, so looking at $_SERVER['SERVER_NAME'] works for me, but obviously won't for you. I simply set up the /etc/hosts file with a lot of aliases, and then they can become virtual hosts. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php