【问题标题】:Methods to determine if PHP is running from a shell or within apache? [duplicate]确定 PHP 是从 shell 还是在 apache 中运行的方法? [复制]
【发布时间】:2011-09-01 20:56:07
【问题描述】:

可能重复:
How to differentiate between http and cli requests?
Which method to detect run mode of php script is more reliable?

确定 php 是从 shell(cron 或其他方式)运行还是在 apache 下运行的最佳方法是什么?

【问题讨论】:

标签: php shell apache2


【解决方案1】:

您可能会发现这很有用:http://php.net/manual/en/function.php-sapi-name.php

【讨论】:

    【解决方案2】:
    echo php_sapi_name()==="apache2handler"; //running under apache?
    

    http://sandbox.phpcode.eu/g/5f9d5

    【讨论】:

      【解决方案3】:

      如果您需要知道所使用的确切 SAPI,请参阅 marcelog 的回答。

      如果您只需要知道是否有可用的 HTTP 环境,您可以简单地测试$_SERVER['HTTP_HOST']

      $is_web = isset($_SERVER['HTTP_HOST']);
      

      【讨论】:

        猜你喜欢
        • 2023-01-21
        • 1970-01-01
        • 1970-01-01
        • 2013-02-27
        • 2012-10-23
        • 2011-07-17
        • 1970-01-01
        • 1970-01-01
        • 2011-06-17
        相关资源
        最近更新 更多