2

在我专用的 CentOS linux 服务器上,我的 PHP CLI 已停止工作。我有一个需要 CLI 的开发人员编写的脚本,这些脚本一直工作到昨天。同时发生的另一个怪事是 /usr/local/bin/ 到 php 的符号链接丢失了。我重新创建了它,但它仍然无法正常工作。

我的开发人员说它不起作用,因为当我们从命令行运行这个脚本时;

<?php
echo 'php_sapi_name()='. php_sapi_name() .'<br />';
if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
echo "ENVIRONMENT IS CLI";
} ELSE {
echo "ENVIRONMENT IS NOT CLI";
}
//echo phpinfo();

然后从命令行返回的是

php_sapi_name()=cgi-fcgi<br />ENVIRONMENT IS NOT CLI

运行 php -v 给我以下输出

PHP 5.3.8 (cgi-fcgi) (built: Feb  2 2012 14:48:13)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
with Zend Guard Loader v3.3, Copyright (c) 1998-2010, by Zend Technologies

最终改变了我们的脚本来解决这个问题,但它是如何改变的仍然很奇怪

4

1 回答 1

0

尝试这个:

<?php
class CliCheck
{
    /**
     * Advanced PHP-CLI mode check.
     *
     * @return boolean    Returns true if PHP is running from the CLI or else false.
     *
     * @access public
     * @static
     */
    public static function isCli()
    {
        // If STDIN was not defined and PHP is running as CGI module
        // we can test for the environment variable TERM. This
        // should be a right way how to test the circumstance under
        // what mode PHP is running.
        if(!defined('STDIN') && self::isCgi()) {
            // STDIN was not defined, but if the environment variable TERM
            // is set, it is save to say that PHP is running from CLI.
            if(getenv('TERM')) {
                return true;
            }
            // Now return false, because TERM was not set.
            return false;
        }
        return defined('STDIN');
    }   

    /**
     * Simple PHP-CGI mode check.
     *
     * (DSO = Dynamic Shared Object)
     *
     * @link http://httpd.apache.org/docs/current/dso.html DSO
     * @link http://www.php.net/manual/en/function.php-sapi-name.php PHP_SAPI
     *
     * @return boolean    Returns true if PHP is running as CGI module or else false.
     *
     * @access public
     * @static
     */
    public static function isCgi()
    {
        if (substr(PHP_SAPI, 0, 3) == 'cgi') {
            return true;
        } else {
            return false;
        }
        return false;
    }
}
print "PHP is running ".((CliCheck::isCgi()) ? "as CGI modul " : ((!CliCheck::isCli()) ? "as DSO modul " : ""));
print ((CliCheck::isCli()) ? "from the CLI." : ".");
print "\n";
?>

来源:http ://www.php.net/manual/fr/features.commandline.php#105043

于 2012-08-24T11:03:42.700 回答