【问题标题】:How to execute a php script from another php script?如何从另一个 php 脚本执行 php 脚本?
【发布时间】:2014-11-27 06:28:02
【问题描述】:

我有一些名为 file1.php、file2.php、file3.php 的 PHP 文件。现在,我想从名为 all.php 的其他 PHP 文件中一一运行这些 PHP 文件。我的意思是 file2.php 将在 file1.php 完成后执行。 file3.php 将在 file2.php 完成后执行。该怎么做?

我可以使用 exec 函数吗?我的主机安全吗?我在共享主机中使用 Cpanel。有什么方法可以做到这一点,但对我的托管内容安全吗?

非常感谢!

【问题讨论】:

  • 在 all.php 中包含你需要的内容

标签: php shell security exec


【解决方案1】:

你可以使用include()

include('file1.php');
include('file2.php');
include('file3.php');

include_once()

include_once('file1.php');
include_once('file2.php');
include_once('file3.php');

requirerequire_once

require 'file1.php';
require 'file2.php';
require 'file3.php';

=> require() 将产生致命错误 (E_COMPILE_ERROR) 并停止脚本

=> include() 只会产生警告 (E_WARNING) 并且脚本会继续运行

【讨论】:

    【解决方案2】:

    在文件all.php你可以这样做:

    include('file1.php');
    include('file2.php');
    include('file3.php');
    

    【讨论】:

    • 当你包含文件的时候,就好像你把文件写在你包含它的位置!!!!
    【解决方案3】:

    是的,您可以使用 'exec' 功能或作为备用 'system'、'passthru' 功能

    但在这种情况下,您的文件,即“file1.php”、“file2.php”和“file3.php”

    • 将作为 cli 执行。
    • 您不会获得诸如 $_REQUEST、$_SESSION 等 http 变量。
    • 安全性取决于您在 file1.php 中编写的代码。
    • 如果您从 web url 调用 all.php,您的代码将作为 apache 用户的权限执行(所以它可能是安全的)。

    【讨论】:

      【解决方案4】:

      如果你想执行一个完全独立的脚本,你可以使用 exec (http://php.net/manual/en/function.exec.php)

      【讨论】:

        最近更新 更多