简体   繁体   English

从 PHP 中检测虚拟主机或 localhost/subdir?

[英]Detect virtual host or localhost/subdir from PHP?

I'm trying out a lot of PHP example projects.我正在尝试很多 PHP 示例项目。 For most examples I need to setup a VirtualHost in the Apache config ( httpd-vhosts.conf ) to get them working.对于大多数示例,我需要在 Apache 配置 ( httpd-vhosts.conf ) 中设置一个 VirtualHost 以使它们正常工作。

But sometimes I just want to copy or test a project, without going through the hassle of making another VirtualHost.但有时我只想复制或测试一个项目,而无需费心制作另一个 VirtualHost。 It would be nice if everything still works too when I try http://localhost/ProjectDir/ instead of http://ProjectDir/如果我尝试http://localhost/ProjectDir/而不是http://ProjectDir/时一切仍然正常,那就太好了

In Phalcon I know how to fix the root directory, by changing the config or with setBaseUri (), but this is a bit inconvenient to change every time.在 Phalcon 中,我知道如何通过更改配置或使用setBaseUri () 来修复根目录,但是每次更改都有点不方便。 It would be nice if this could be done automatically somehow.如果这能以某种方式自动完成,那就太好了。 (I do realize that in this way I can not use absolute URLs or links inside the HTML (starting with / ) since that would mean the root folder of the Apache server then but this could be solved with an automatically configured prefix-variable). (我确实意识到,通过这种方式,我不能在 HTML 中使用绝对 URL 或链接(以/开头),因为这意味着 Apache 服务器的根文件夹,但这可以通过自动配置的前缀变量来解决)。

  • How do other developers handle this situation, or do you really choose between a virtual host or http://localhost/ProjectDir/ ?其他开发人员如何处理这种情况,或者您真的在虚拟主机或http://localhost/ProjectDir/之间进行选择?

  • Is there a way for a PHP script to detect if it's running in a virtual host or in a subdir of localhost?有没有办法让 PHP 脚本检测它是在虚拟主机中运行还是在 localhost 的子目录中运行?

UPDATE更新

Found out I could use $_SERVER['SERVER_NAME'] to determine if it's ' localhost ' or something else.发现我可以使用$_SERVER['SERVER_NAME']来确定它是“ localhost ”还是其他东西。 Not very elegant, but it's a sufficient solution, I guess.不是很优雅,但我想这是一个足够的解决方案。

Within html views:在 html 视图中:

I like to use absolute paths from the document root for ease.为了方便起见,我喜欢使用文档根目录中的绝对路径。 (Relative paths are flexible but can get complicated.) (相对路径很灵活,但可能会变得复杂。)

<img src="/images/foo.jpg">

Of course if I want to move the project into a sub directory, the path above will need to be adjusted.当然如果我想将项目移动到子目录中,则需要调整上面的路径。 (We could use the html base tag, but that can have side effects .): (我们可以使用 html 基本标签,但这会产生副作用。):

<img src="/subdir/images/foo.jpg">

We could use a variable in our views that we prefix to links and asset paths:我们可以在视图中使用一个变量作为链接和资产路径的前缀:

<?php
     $base = '/subdir'; // Set somewhere.
?>
<img src="<?php echo $base ?>/images/foo.jpg">

To set the $base variable automatically, I sometimes assign it to Symfony/Component/HttpFoundation/Request::getBasePath() .为了自动设置$base变量,我有时将它分配给Symfony/Component/HttpFoundation/Request::getBasePath() (There are some other useful utility methods in that component.) Or use something similar from another framework/library/component. (该组件中还有一些其他有用的实用方法。)或者使用来自另一个框架/库/组件的类似内容。

Assigning a value in one place in a central configuration isn't that much of a hardship.在中央配置的一个地方分配一个值并不是那么困难。 I find prefixing paths within the views more of a pain.我发现在视图中添加前缀路径更痛苦。

In one of my development environments, I use a symlink from the directory root to the project directories directory root:在我的一个开发环境中,我使用从目录根目录到项目目录根目录的符号链接:

/var/www -> /sites/example.com/pub

And swap it as and when needed.并在需要时交换它。 You can avoid writing multiple virtual host configs that way and skip the other steps mentioned above.您可以避免以这种方式编写多个虚拟主机配置并跳过上面提到的其他步骤。

However, configurable prefixes can be useful in other ways:但是,可配置的前缀在其他方面也很有用:

$base_cdn    = 'http://cdn.example.com';
$base_nav    = '/subdir';
$base_static = '//static.example.com/foo';

assuming the entry is index.php假设条目是 index.php

function getVirtualHostPath(): string
{
    $temp = str_replace("index.php", "", $_SERVER["SCRIPT_NAME"]);
    return substr($temp, 1);
}
// "https://domain.example/subdir/api/v2/store/inventory" => "subdir/"
// "https://domain.example/api/v2/store/inventory" => "" 
// "https://domain.example/subdir/api/v2/store/inventory" => "subdir/"
// "https://127.0.0.1/api/v2/store/inventory" => ""

Explanation:解释:

  1. dump $_SERVER super global in production (that uses virtual hosts) and filter out all keys that does not contain "subdir".在生产中转储$_SERVER超级全局(使用虚拟主机)并过滤掉所有不包含“subdir”的键。
  2. filter out all keys that are not present in CGI 1.1 specification过滤掉所有不在CGI 1.1 规范中的键
  3. copy the same keys from development environment without virtual host.从没有虚拟主机的开发环境中复制相同的密钥。
  4. $_SERVER["SCRIPT_NAME"] contains "/subdir/index.php" in prod and "/index.php" in dev. $_SERVER["SCRIPT_NAME"]在 prod 中包含“/subdir/index.php”,在 dev 中包含“/index.php”。 Removing "index.php" will return the virtual host path.删除“index.php”将返回虚拟主机路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM