确定脚本所在的服务器以及PHP中的配置的最佳方法是什么? [英] What is the best way to determine which server the script is on and therefore the configuration in PHP?

查看:124
本文介绍了确定脚本所在的服务器以及PHP中的配置的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试确定使用PHP脚本确定当前正在运行脚本/网站的服务器的最佳方式。

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

目前我有一个 switch()使用 $ _ SERVER ['SERVER_NAME']。 ':'。 $ _SERVER ['SERVER_PORT'] 以确定它所在的服务器。然后根据它所在的服务器设置几个路径,db连接参数,SMTP参数和调试设置。

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.)

这意味着我可以简单地将站点删除到任何配置的服务器,而不必更改任何代码(具体来说,配置)。如果它是一个新的服务器,那么我只是添加一个新的 case ,然后就可以了。

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.

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

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 。这意味着我们需要暂时评论一个案例,并确保它不进入仓库。

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 ['SERVER_ADDR' ] 并将您的身份基于服务器的IP地址。

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

更新:在虚拟主机情况下,您可能还想将IP与文档根路径如下:

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

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

这篇关于确定脚本所在的服务器以及PHP中的配置的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆