Determine whether PHP is being run via HTTP or CLI - Follow Up
Posted March 9th, 2008 in PHP
As a quick follow up to yesterday's post about how to determine whether PHP is being run via HTTP or CLI, I decided to do a quick post about where I found the values in the source code which are returned by the php_sapi_name() function and PHP_SAPI constant.
I downloaded the PHP source code for PHP 5.2.5 and extracted it to a temporary directory, and first did a grep on "cli" to see if I could work out where the values for php_sapi_name and PHP_SAPI were defined. I discovered they are defined in a subdirectory off the main directory called "sapi". This "sapi" directory had a further subdirectory for each sapi type, and only one of these will be compiled into PHP, depending on the configure compile settings.
In PHP 5.2.5 the subdirectories under sapi are as follows:
aolserver apache apache2filter apache2handler apache_hooks caudium cgi cli continuity embed isapi milter nsapi phttpd pi3web roxen tests thttpd tux webjames
With the exception of the "tests" subdirectory, each has several files including one which contains a definition for the sapi_module_struct structure.
An example of this is:
static sapi_module_struct apache_sapi_module = {
"apache", /* name */
"Apache", /* pretty name */
...
The value under /* name */ is what is returned by the php_sapi_name function and PHP_SAPI constant, so it was just a matter of looking in the appropriate file in each subdirectory and finding what the name was set to for each one. This is how I compiled the list for the how to determine whether PHP is being run via HTTP or CLI post.
This is the list of files and the name value from each one:
- aolserver/aolserver.c - aolserver
- apache/mod_php5.c - apache
- apache2filter/sapi_apache2.c - apache2filter
- apache2handler/sapi_apache2.c - apache2handler
- apache_hooks/mod_php5.c - apache
- caudium/caudium.c - caudium
- cgi/cgi_main.c - cgi-fcgi OR cgi (depends if PHP_FASTCGI is defined)
- cli/php_cli.c - cli
- continuity/capi.c - Continuity
- embed/php_embed.c - embed
- isapi/php5isapi.c - isapi
- milter/php_milter.c - milter
- nsapi/nsapi.c - nsapi
- phttpd/phttpd.c - phttpd
- pi3web/pi3web_sapi.c - pi3web
- roxen/roxen.c - roxen
- thttpd/thttpd.c - thttpd
- tux/php_tux.c - tux
- webjames/webjames.c - webjames
Related posts:
- Running PHP scripts as shell scripts (Monday, February 2nd 2009)
- PHP CLI counter for long running processes (Friday, August 29th 2008)
- Command line arguments for the PHP CLI (Thursday, June 19th 2008)
- Command line arguments with a PHP CLI script (Sunday, June 15th 2008)
- Determine whether PHP is being run via HTTP or CLI (Saturday, March 8th 2008)
- Man Pages - Manpage for chmod (Wednesday, May 26th 2004)

Comments
blog comments powered by Disqus