简体   繁体   English

PHP 脚本检测到它是一个包含

[英]PHP script detect that it is an include

I have a PHP class file that is used as an include both through a web server and by a cron process.我有一个 PHP class 文件,它通过 web 服务器和 cron 进程用作包含。 I am looking for a way to add some code to the head of the script so that I can detect if the script is being launched directly from the command line instead of being included in another script.我正在寻找一种方法将一些代码添加到脚本的头部,以便我可以检测脚本是否直接从命令行启动而不是包含在另一个脚本中。 This way I can make testing a bit easier by calling the script directly and having a function instantiate the object and execute some code on it without needing to create a wrapper script for it.这样我可以通过直接调用脚本并让 function 实例化 object 并在其上执行一些代码而无需为其创建包装脚本,从而使测试更容易一些。

I tried using if (php_sapi_name() == 'cli') { but that tests true even if the script is being included in another script that was called from the command line.我尝试使用if (php_sapi_name() == 'cli') {但即使该脚本包含在从命令行调用的另一个脚本中,该测试也是如此。

Checking that $argv[0] is set lets you know if things were invoked from the command line; 检查$ argv [0]是否设置可让您知道是否从命令行调用了这些内容。 it does not tell you if the file was invoked directly or if it was included from another file. 它不会告诉您是直接调用文件还是从另一个文件包含文件。 To determine that, test if realpath($argv[0]) == realpath(__FILE__) . 要确定这一点,请测试realpath($argv[0]) == realpath(__FILE__)

Putting it all together: 放在一起:

if (isset($argv[0]) && realpath($argv[0]) == realpath(__FILE__))
    // This file was called directly from command line

You can check for the presence of $argv . 您可以检查是否存在$argv If $argv[0] is set (the script name), your script is run from the command line. 如果设置了$argv[0] (脚本名称),则从命令行运行脚本。

You could test if $_SERVER['DOCUMENT_ROOT'] is empty. 您可以测试$_SERVER['DOCUMENT_ROOT']是否为空。 It will be empty in the command line execution since it's a variable predefined by apache. 由于它是apache预定义的变量,因此在命令行执行中将为空。

What's interesting is $_SERVER['DOCUMENT_ROOT'] is defined but empty. 有趣的是$_SERVER['DOCUMENT_ROOT']已定义但为空。 I would have guessed it wouldn't be defined at all when using the cli. 我猜想在使用cli时根本不会定义它。

You could also test if $argv is defined or its size (at least 1 when using CLI). 您还可以测试是否定义了$argv或其大小(使用CLI时至少为1)。 I didn't test when including the file but if defined, sizeof($argv) would definitely be 0. 我没有测试何时包含文件,但如果定义, sizeof($argv)肯定为0。

Other possible tests are $_SERVER['argc'] (0 when executed by a server, 1 when executed from CLI) and a strange $_SERVER['_'] defined to the path to the PHP binary, which is not defined at all when served. 其他可能的测试是$_SERVER['argc'] (由服务器执行时为0,从CLI执行时为1)和为PHP二进制文件的路径定义的奇怪的$_SERVER['_'] ,但根本没有定义当服。

To conclude, I would rely on $_SERVER['argc'] (or $argc ) which is a direct count of the number of arguments passed to the command line: will always be 0 when the script is served. 总而言之,我将依赖$_SERVER['argc'] (或$argc ),它是传递给命令行的参数数量的直接计数:服务脚本时始终为0。 Relying on $argv is more complicated: you have to test if $argv[0] is set or if sizeof($argv) is > 0. 依靠$ argv更为复杂:您必须测试是否设置了$argv[0]sizeof($argv) > 0。

I have successfully used if (defined('STDIN')) as such a check. 我已经成功地使用if (defined('STDIN'))这样的检查。 STDIN will be defined if the script is run from a shell, but not if it's in a server environment. 如果脚本是从外壳运行的,则将定义STDIN ,但如果是在服务器环境中则不会定义。

As you can see here and at the link Ryan provided in his comment, there are lots of possibilities. 正如您在此处以及Ryan在其评论中提供的链接所看到的,存在很多可能性。

if($_SERVER['SCRIPT_FILENAME']==__FILE__){
   // Directly
}else{
   // Included
}

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

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