php 用php获取主机名的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1408996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:27:07  来源:igfitidea点击:

Best way to get hostname with php

php

提问by mattweg

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 unique and reliable system identifier. Here's what we have thought of so far:

我有一个安装在多台服务器和我们所有开发人员笔记本电脑上的 php 应用程序。我需要一种快速可靠的方法来获取服务器的主机名或其他一些唯一且可靠的系统标识符。到目前为止,这是我们想到的:

<? $hostname = (!empty($_ENV["HOSTNAME"])) ? $_ENV["HOSTNAME"] : env('HOSTNAME'); ?>

<? $hostname = (!empty($_ENV["HOSTNAME"])) ? $_ENV["HOSTNAME"] : env('HOSTNAME'); ?>

<? $hostname = gethostbyaddr($_SERVER['SERVER_ADDR']); ?>

<? $hostname = gethostbyaddr($_SERVER['SERVER_ADDR']); ?>

<? $hostname = exec('hostname'); ?>

<? $hostname = exec('hostname'); ?>

What do you think?

你怎么认为?

采纳答案by zombat

What about gethostname()?

怎么样的gethostname()

Edit: This might not be an option I suppose, depending on your environment. It's new in PHP 5.3. php_uname('n')might work as an alternative.

编辑:我认为这可能不是一个选项,具体取决于您的环境。它是 PHP 5.3 中的新功能。 php_uname('n')可以作为替代。

回答by ghbarratt

For PHP >= 5.3.0 use this:

对于PHP >= 5.3.0 使用这个

$hostname = gethostname();

$hostname = gethostname();

For PHP < 5.3.0 but >= 4.2.0 use this:

对于PHP < 5.3.0 但 >= 4.2.0 使用这个

$hostname = php_uname('n');

$hostname = php_uname('n');

For PHP < 4.2.0 use this:

对于 PHP < 4.2.0 使用这个:

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

回答by Pedro

You could also use...

你也可以用...

$hostname = getenv('HTTP_HOST');

回答by Hammad Khan

The accepted answer gethostname() may infact give you inaccurate value as in my case

接受的答案 gethostname() 实际上可能会给你不准确的值,就像我的情况一样

gethostname()         = my-macbook-pro     (incorrect)
$_SERVER['host_name'] = mysite.git         (correct)
gethostname()         = my-macbook-pro     (incorrect)
$_SERVER['host_name'] = mysite.git         (correct)

The value from gethostname() is obvsiously wrong. Be careful with it.

gethostname() 的值显然是错误的。小心点。

Update as corrected by the comment

根据评论更正更新

Host name gives you computer name, not website name, my bad. My result on local machine is

主机名给你计算机名,而不是网站名,我不好。我在本地机器上的结果是

gethostname()         = my-macbook-pro     (which is my machine name)
$_SERVER['host_name'] = mysite.git         (which is my website name)

回答by Mike Grace

I am running PHP version 5.4 on shared hosting and both of these both successfully return the same results:

我在共享主机上运行 PHP 5.4 版,这两个都成功返回了相同的结果:

php_uname('n');

gethostname();

回答by RageZ

php_unamebut I am not sure what hostname you want the hostname of the client or server.

php_uname但我不确定您想要客户端或服务器的主机名是什么主机名。

plus you should use cookie based approach

另外你应该使用基于 cookie 的方法