命令行中的 PHP 服务器名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/626416/
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-24 23:18:20  来源:igfitidea点击:

PHP Server Name from Command Line

phpcommand-linecommand-line-interface

提问by Justin Noel

Is there a way to detect the name of the server running a PHP script from the command line?

有没有办法从命令行检测运行 PHP 脚本的服务器的名称?

There are numerous ways to do this for PHP accessed via HTTP. But there does not appear to be a way to do this for CLI.

对于通过 HTTP 访问的 PHP,有多种方法可以做到这一点。但似乎没有办法为 CLI 做到这一点。

For example:

例如:

$_SERVER['SERVER_NAME'] 

is not available from the command line.

无法从命令行使用。

回答by Alexander V. Ilyin

<?php
echo gethostname(); // may output e.g,: sandie

See http://php.net/manual/en/function.gethostname.php

http://php.net/manual/en/function.gethostname.php

gethostname() is a convenience function which does the same as php_uname('n')and was added as of PHP 5.3

gethostname() 是一个方便的函数,它的作用与php_uname('n')PHP 5.3相同,并且是从 PHP 5.3 开始添加的

回答by mikl

The SERVER_NAME is not available when you run PHP from the CLI for that very same reason.

出于同样的原因,当您从 CLI 运行 PHP 时,SERVER_NAME 不可用。

When you run PHP from the CLI, you start your own PHP intepreter that runs whatever code you passed to it, without any kind of server. So from the CLI, PHP knows nothing about your web server that you do not explicitly tell it.

当您从 CLI 运行 PHP 时,您将启动您自己的 PHP 解释器,它运行您传递给它的任何代码,而无需任何类型的服务器。因此,从 CLI 来看,PHP 对您的 Web 服务器一无所知,您没有明确告诉它。

回答by jonstjohn

Try:

尝试:

$servername = trim(`hostname`);

回答by trond

Call the CLI and use the gethostname command:

调用 CLI 并使用 gethostname 命令:

php -r "echo gethostname();"

Prints:

印刷:

your_hostname

回答by Menios

In order to get the hostname from the commandlineyou would need to also run using php -r. On linux I used:

为了得到从主机名的命令行,你就还需要使用PHP -r运行。在 linux 上我用过:

~#php -r 'echo php_uname("n");'
hostname

On windows I used:

在我使用的 Windows 上:

D:\xampp\php>php -r "echo php_uname('n');"
MB-PC

Additionally, when accessed via the console PHP does not provide many of the classic $_SERVER values but on the server I accessed only has the following keys:

此外,当通过控制台访问时,PHP 不提供许多经典的 $_SERVER 值,但在我访问的服务器上只有以下键:

root@hermes:~# php -r 'foreach($_SERVER as $key =>$value){echo $key.",";}'
TERM,SHELL,SSH_CLIENT,SSH_TTY,USER,LS_COLORS,MAIL,PATH,PWD,LANG,SHLVL,HOME,LOGNAME,SSH_CONNECTION,LESSOPEN,LESSCLOSE,_,PHP_SELF,SCRIPT_NAME,SCRIPT_FIL
ENAME,PATH_TRANSLATED,DOCUMENT_ROOT,REQUEST_TIME,argv,argc

回答by Vladyslav Litunovsky

You can add your php script to command line that will set $_SERVER['SERVER_NAME'] for you. Here is how I'm using it:

您可以将您的 php 脚本添加到将为您设置 $_SERVER['SERVER_NAME'] 的命令行。这是我如何使用它:

C:\SDK\php-5.5.25-Win32-VC11-x86\php.exe -S localhost:8080 -t D:\Projects\Sites\mysite router.php

C:\SDK\php-5.5.25-Win32-VC11-x86\php.exe -S localhost:8080 -t D:\Projects\Sites\mysite router.php

router.php

路由器文件

<?php
$_SERVER['SERVER_NAME'] = 'localhost:8080';
return false;    // serve the requested resource as-is.
?> 

回答by Vladyslav Litunovsky

Possibly because if you run a script from the command line, no server is involved?

可能是因为如果您从命令行运行脚本,则不涉及服务器?

回答by ebonit

One of these may have the server name:

其中之一可能具有服务器名称:

print_r($_SERVER)
var_dump($_SERVER) 
echo $_SERVER['HOSTNAME']

Gives me the name of the server.

给我服务器的名称。