如何区分环境是否是虚拟主机?

发布于 2024-11-17 00:08:31 字数 349 浏览 5 评论 0原文

我这里有这段代码:

$config['SUBFOLDER'] = '/';
$config['APP_URL'] = 'http://'.$_SERVER['HTTP_HOST'].$config['SUBFOLDER'];

APP_URL 在整个 HTML 模板中使用。问题是 - 配置需要尽可能通用,因此切换环境时要做的事情较少。

现在,当我为我的项目配置虚拟主机时,它的工作原理是这样的,但是当它不是虚拟主机,而是某种 localhost/myproject/ 时 - $config['SUBFOLDER'] 必须手动设置为/myproject/

如何以编程方式执行此操作?

I have this code here:

$config['SUBFOLDER'] = '/';
$config['APP_URL'] = 'http://'.$_SERVER['HTTP_HOST'].$config['SUBFOLDER'];

And APP_URL is used throughout the HTML templates. The problem is - the config needs to be as universal as possible, so there's less to do when switching environments.

Now, it works like this when I have configured a virtual host for my project, but when it isn't a virtual host, but sort of a localhost/myproject/ - the $config['SUBFOLDER'] has to be set manually to /myproject/

How do I do this programmatically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情域 2024-11-24 00:08:31

例如,您可以在 htaccess 文件中设置一个变量。

#.htccess
SetEnv APPLICATION_ENV development

然后在您的网站中对此进行检查:

<?php
//live
$config['SUBFOLDER'] = '/';
//on localhost - override live settings
if(APPLICATION_ENV === 'development')
{
  $config['SUBFOLDER'] = '/myproject';
}

使用这种方法,您只需更改 htaccess 文件中的一个变量。

如果您只想检查是否在本地主机上运行,​​则检查 $_SERVER['HTTP_HOST'] 也可以:

<?php
if($_SERVER['HTTP_HOST'] === 'localhost')
{
  $config['SUBFOLDER'] = '/myproject';
}

You could set a variable in, for example, your htaccess file.

#.htccess
SetEnv APPLICATION_ENV development

then in your website do a check for this:

<?php
//live
$config['SUBFOLDER'] = '/';
//on localhost - override live settings
if(APPLICATION_ENV === 'development')
{
  $config['SUBFOLDER'] = '/myproject';
}

With this approach you only need to change one variable in the htaccess file.

If you just want to check if you are running on localhost, doing a check on $_SERVER['HTTP_HOST'] will also work:

<?php
if($_SERVER['HTTP_HOST'] === 'localhost')
{
  $config['SUBFOLDER'] = '/myproject';
}
↙厌世 2024-11-24 00:08:31

检查 URL 是否包含 LOCALHOST 还是普通域名,并将您的配置包装在依赖于 URL 的条件中。

Check the URL whether it contains LOCALHOST or a normal domain name and wrap your config up in a conditional dependent on what the URL is.

很酷不放纵 2024-11-24 00:08:31

我采取稍微不同的方法..

if ($_SERVER['HTTP_HOST'] == 'testingserver') // local environment here
{
    // local settings
}
else if ( // remote environment here
    $_SERVER['HTTP_HOST'] == 'www.something.com' ||
    $_SERVER['HTTP_HOST'] == 'something.com'
) 
{
    // remote settings
}
else // error.. this shouldn't happen
{
    echo $_SERVER['HTTP_HOST']; 
}

I take a slightly different approach..

if ($_SERVER['HTTP_HOST'] == 'testingserver') // local environment here
{
    // local settings
}
else if ( // remote environment here
    $_SERVER['HTTP_HOST'] == 'www.something.com' ||
    $_SERVER['HTTP_HOST'] == 'something.com'
) 
{
    // remote settings
}
else // error.. this shouldn't happen
{
    echo $_SERVER['HTTP_HOST']; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文