我们如何检查 PHP 脚本是否在本地主机上运行?

发布于 2024-10-13 15:24:41 字数 384 浏览 5 评论 0原文

有没有办法检查我们是否在本地主机(开发服务器)或实时服务器(生产服务器)上运行 PHP 脚本?有没有 PHP 常量、变量、函数等可以给我这些信息。

我需要它为生产和开发服务器设置不同的设置。现在我解析 URL 以查看它是哪一个,但我想知道是否有更好的方法可以做到这一点。我担心的是我们可能会更改脚本的 URL,这可能会破坏我的检查。

我正在寻找一种包含一个配置文件和 IF 条件的解决方案,具体取决于我将定义不同的设置。唯一的问题是,当服务器设置(例如主机名、document_root 或我用来识别本地/远程主机的其他内容)发生更改时,我不想更改 IF 语句。

并希望从一个源进行 SVN 更新,而不对我的生产服务器进行任何更改。

理想情况下,我希望能够使用这些设置运行 CRON 作业。

Is there a way to check if we are running a PHP script on localhost - development server or on live server - production server? Is there any PHP constant, variable, function,etc that can give me this information.

I need this to put different settings for production and development server. Now I parse the URL to see which one it is but I was wondering s there any better way to do that. My concern is that we may change the URL of the script and that may ruin my check.

I am looking few a solution with one config file and IF condition in it depending on which I will define different settings. The only problem is that I do not want to change the IF statement when there are changes on the server settings like hostname, document_root or something else that I am using to identify local/remote host.

And want to SVN update from one source without changing anything to my production server.

And I would like ideally to be able to run and CRON jobs with these settings.

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

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

发布评论

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

评论(10

寒冷纷飞旳雪 2024-10-20 15:24:41

我在主机定义中使用 SetEnv 来定位我正在运行的环境(Dev、Stage、Production):

<VirtualHost *:80>
(all the host info)
SetEnv SERVER_CONTEXT "dev"
</VirtualHost>

每个配置文件作为其中的一个额外单词:config.dev.ini、config.stage.ini、config.prod .ini 等...
它就像一个魅力。

I use the SetEnv in my host definition to locate on which environment i am running (Dev, Stage, Production) :

<VirtualHost *:80>
(all the host info)
SetEnv SERVER_CONTEXT "dev"
</VirtualHost>

And each config file as an extra word in it : config.dev.ini, config.stage.ini, config.prod.ini, etc, ...
It works like a charm.

近箐 2024-10-20 15:24:41
if($_SERVER["REMOTE_ADDR"]=="127.0.0.1"){
$local = True;
}else{

    $local = False;
}

编辑

您还可以检查地址的第一部分并查看服务器是否在本地网络中,再次假设您的服务器在生产时不会在本地网络中

if($_SERVER["REMOTE_ADDR"]=="127.0.0.1"){
$local = True;
}else{

    $local = False;
}

EDIT

You could also check the first part of the address and see if the server is in the local network, the again assuming your server won't be in the local network when in production

不疑不惑不回忆 2024-10-20 15:24:41

我在 Apache 配置中设置了一个环境变量并进行了检查。与使用 PHP 配置文件相比,这有一个优点,即所有应用程序代码在 PROD、TEST、DEV 等上都保持完全相同;签出后无需进行更改,因为代码只是从 Apache 中提取配置。

为了演示,可以在您的 VirtualHost 配置中设置以下内容

SetEnv ENVIRONMENT PROD

在您的 PHP 代码中,您可以使用以下命令检查环境

$env = getenv('ENVIRONMENT');

如果您觉得需要使其在任何地方都可用,则可以使用 define,但我觉得没有必要(我使用指定的环境加载适当的配置文件并创建只读的单例配置,然后用它来检查任何配置选项;比 if ($env == 'PROD ') {} 类型代码,因为所有逻辑都在配置中,而不是在您的代码中)。

I set an environment variable in the Apache configuration and check that. This has the advantage over using a PHP configuration file that all your application code remains exactly the same on PROD, TEST, DEV etc; no need to go and make changes after a check out, as the code just pulls the config from Apache.

To demonstrate, the following can be set in your VirtualHost configuration

SetEnv ENVIRONMENT PROD

In your PHP code, you can then check the environment with

$env = getenv('ENVIRONMENT');

If you feel the need to make this available everywhere, you can then use define, but I don't find it necessary (I use the specified environment to load the appropriate configuration files and create a read-only Singleton configuration, which is then used to check any configuration options; better than if ($env == 'PROD') {} type code, as all that logic is in the config, not in your code).

往日 2024-10-20 15:24:41

使用包含在其中的定义的配置文件

define("DEBUG",true); //set to false for live

,并在代码中使用它,例如:

if(DEBUG){}

Use a config file you include with a define in it

define("DEBUG",true); //set to false for live

and use that in your code, e.g.:

if(DEBUG){}
梦里泪两行 2024-10-20 15:24:41

您可以尝试使用全局 $_SERVER['SERVER_NAME' ] 这应该告诉您正在运行脚本的系统的主机名。您可以使用它来确定要使用的设置(取决于系统)。

我不知道这会输出“localhost”,但是您可能需要知道开发计算机的实际主机名。

You can try and use the global $_SERVER['SERVER_NAME'] This should tell you the host name of the system that's running the script. You could use this to determine what settings to use (depending on the system).

I don't know that this will output 'localhost' however and you may need to know your actual host name of your development machine.

困倦 2024-10-20 15:24:41

除非您告诉服务器,否则服务器不知道它是什么环境。我所做的是使用 DEFINE 设置环境。我的应用程序代码在每个实例上都是相同的,但我的配置文件发生了变化。这样您就可以使用 .htaccess 文件包含每个脚本上的配置文件并检查它们的设置是什么。

The server has no idea what environment it is unless you tell it to. What I do is use DEFINE to set the environment. My application code is the same on every instance but the my configuration files change. That way you can use .htaccess file include the configuration files on every script and check to see what they settings are.

給妳壹絲溫柔 2024-10-20 15:24:41

我知道这个词对于 PHP 开发人员来说可能听起来很奇怪,但是您是否考虑过构建您的项目?

在 PHP 中,不需要编译任何内容,但是更改复制的文件是任何构建过程的功能之一。您可以指定 2 个目标:生产开发。不需要任何应该有效或可能有效但在某些情况下不会的条件。

I know this word may sound strange to PHP developer, but have you considered build of your project?

In PHP there's nothing to compile, however changing copied files is one of features of any build process. You could specify 2 targets: production and dev. There would be no need for any conditionals, that should work, or may work, but under some circumstances won't.

烟若柳尘 2024-10-20 15:24:41

我总是制作“config.php”,将其包含到其他 php 文件中...

它包含类似(主文件)之类的内容:

$CONFIG['server']="dev";
$CONFIG['db_user']="root";

以及类似的内容...
所以在家里我有这个,在网站运行的服务器上我有另一个,如果不更改它我不会更新...

所以在服务器上我有类似的东西:

$CONFIG['server']="prod";
$CONFIG['db_user']="lwu9918_admin";

然后在其他 php 文件中:

include("config.php");
if($CONFIG['server']=="dev"){echo "Development";}

类似的东西!

i always make "config.php" wich i include to other php files...

it contains something like (home file):

$CONFIG['server']="dev";
$CONFIG['db_user']="root";

and thing like that...
so at home i have this one up there and at server where site is running i have another one wich i dont update if not changes to it...

so on server i have something like:

$CONFIG['server']="prod";
$CONFIG['db_user']="lwu9918_admin";

and then in other php files:

include("config.php");
if($CONFIG['server']=="dev"){echo "Development";}

thing like that!

凉薄对峙 2024-10-20 15:24:41

在开发服务器上

* * * * * php -r cronjob.php this_is_dev

在生产服务器上

* * * * * php -r cronjob.php this_is_live

在您的脚本中

switch ($argv[1])
  case 'this_is_dev':
    // load your dev configuration
    break;

  case 'this_is_live':
    // load your live configuration
    break;

  default:
    die('invalid server');
    break;
}

,由于用于 cronjob,因此存在 $argv

祝 Windows 好运:(

On dev server

* * * * * php -r cronjob.php this_is_dev

On production server

* * * * * php -r cronjob.php this_is_live

In your script

switch ($argv[1])
  case 'this_is_dev':
    // load your dev configuration
    break;

  case 'this_is_live':
    // load your live configuration
    break;

  default:
    die('invalid server');
    break;
}

since is meant for cronjob, the $argv is exist

good luck with Windows :(

我一向站在原地 2024-10-20 15:24:41

最好将 cli 运行程序放在 www 目录之外,例如

c:\iis\www 是公共 html 目录,

cli 文件应放在 c:\iis 下

you better put the cli running program outside www directory ,for example

c:\iis\www is the public html directory

the cli file should be put under c:\iis instead

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文