I'm trying to determine the best way of having a PHP script determine which server the script/site is currently running on.

我正在尝试确定让PHP脚本确定脚本/站点当前运行的服务器的最佳方法。

At the moment I have a switch() that uses $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] to determine which server it's on. It then sets a few paths, db connection parameters, SMTP paramters and debug settings based on which server it's on. (There maybe additional parameters depending on the site needs.)

目前我有一个使用$ _SERVER ['SERVER_NAME']的开关()。 ':'。 $ _SERVER ['SERVER_PORT']确定它所在的服务器。然后根据它所在的服务器设置几个路径,数据库连接参数,SMTP参数和调试设置。 (根据站点需要,可能还有其他参数。)

This means that I can simply drop the site onto any of the configured servers without having to change any code (specifically the configuration). If it's a new server, then I simply add a new case and it's ready from then on.

这意味着我可以简单地将站点放到任何已配置的服务器上,而无需更改任何代码(特别是配置)。如果它是一个新的服务器,那么我只需添加一个新的案例,从那时起就可以了。

We have done loading config files based on the same SERVER_NAME:SERVER_PORT combination, but found that it's another file you have to maintain, plus we weren't sure on the speed of parsing ini files, (although having extra cases for each server may be just as slow).

我们已经完成了基于相同的SERVER_NAME:SERVER_PORT组合加载配置文件,但发现它是你必须维护的另一个文件,而且我们不确定解析ini文件的速度,(尽管每个服务器都有额外的情况可能是一样慢)。

Another problem we have is when a site is often moved between 2 servers, but we use the same SERVER_NAME and SERVER_PORT on each. This means we need to temporarily comment one case and ensure it doesn't get into the repo.

我们遇到的另一个问题是,站点经常在两台服务器之间移动,但我们在每台服务器上使用相同的SERVER_NAME和SERVER_PORT。这意味着我们需要临时评论一个案例并确保它不会进入回购。

Another other ideas? It needs to be available on all servers (sometimes SERVER_NAME and SERVER_PORT are not). It would also be nice if it worked with the CLI PHP.

另一个想法?它需要在所有服务器上可用(有时SERVER_NAME和SERVER_PORT不可用)。如果它与CLI PHP一起工作也会很好。

8 个解决方案

#1


2

How about using $_SERVER['SERVER_ADDR'] and base your identity off the IP address of the server.

如何使用$ _SERVER ['SERVER_ADDR']并将您的身份基于服务器的IP地址。

UPDATE: In a virtual host situation, you might also like to concatenate the IP with the document root path like so:

更新:在虚拟主机情况下,您可能还希望将IP与文档根路径连接起来,如下所示:

$id = $_SERVER['SERVER_ADDR'] . $_SERVER['DOCUMENT_ROOT'];

更多相关文章

  1. PHP - 在外部服务器上读取文件?
  2. 使用GMail SMTP服务器从PHP页面发送电子邮件
  3. PHP项目转移到服务器上验证码不显示解决办法
  4. php如何跨站抓取别的站点的页面的补充
  5. 如何在PHP 5.4.33 Apache服务器上启用mbstring?
  6. 通过添加3hrs从服务器中重新获取CURTIME()并在PHP页面中显示它
  7. php和django位于同一个lighttpd服务器上
  8. 将node.js服务器更改为Apache服务器
  9. 当我将它移动到另一台服务器时,计数器将无法工作

随机推荐

  1. android图片放大 缩小 旋转
  2. Layout1.9
  3. Android usb 驱动
  4. Android 查看本机外网IP
  5. AndroidMenifest.xml(Android清单文件)内
  6. Android调用摄像头闪退
  7. Android官方资料--A/B System Updates
  8. c#开发android时layout.axml没有智能提示
  9. 解读ExpandableListView源码样式
  10. 初探 Ubuntu下android NDK