note 105043 added to features.commandline

From: Date: Mon, 25 Jul 2011 02:02:14 +0000
Subject: note 105043 added to features.commandline
Groups: php.notes 
Request: Send a blank email to php-notes+get-181814@lists.php.net to get a copy of this message
Regarding the way about the PHP CLI detection, i found out that a test on STDIN will not deliver correct results in every circumstances. That is if we execute the CGI version of PHP on the command line interface, the "defined('STDIN')"-statement will return false. Here is my method to detect the CLI mode. <?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"; ?> ---- Server IP: 69.147.83.197 Probable Submitter: 94.134.92.243 ---- Manual Page -- http://www.php.net/manual/en/features.commandline.php Edit -- https://master.php.net/note/edit/105043 Del: integrated -- https://master.php.net/note/delete/105043/integrated Del: useless -- https://master.php.net/note/delete/105043/useless Del: bad code -- https://master.php.net/note/delete/105043/bad+code Del: spam -- https://master.php.net/note/delete/105043/spam Del: non-english -- https://master.php.net/note/delete/105043/non-english Del: in docs -- https://master.php.net/note/delete/105043/in+docs Del: other reasons-- https://master.php.net/note/delete/105043 Reject -- https://master.php.net/note/reject/105043 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#181814) next »