views:

801

answers:

4

I have a PHP script that needs to determine if it's been executed via the command-line or via HTTP, primarily for output-formatting purposes. What's the canonical way of doing this? I had thought it was to inspect SERVER['argc'], but it turns out this is populated, even when using the 'Apache 2.0 Handler' server API.

+1  A: 

I think

$_SERVER['REMOTE_ADDR']

will not be populated from the CLI.

Also, all the HTTP_* keys in the $_SERVER superglobal won't be populated from the CLI, or do it the right way hop just mentioned :-)

Vinko Vrsalovic
+20  A: 

use php_sapi_name(). It returns a string, and everything other than "cli" should mean your script is called from a web server.

From v4.2.0 on, there is also the PHP_SAPI predefined constant.

hop
Thanks. I'm intrigued as to why the doc. example inspects the first 3 characters, whilst the description states the string should be exactly "cgi" but, other than that, I think this is perfect.
Bobby Jack
That's PHP for you... It's also very annoying that the docs only give a view examples of possible values, not a complete list. PHP just is no language for serious programmers.
hop
unless, of course, the returned string was 'cgi', which is also indicative of php being executed from the console. As in, whaddayaknow, my case.
Adriano Varoli Piazza
@Adriano: maybe in your case php-cgi is used to execute the script.
hop
@Bobby, the example in the php.net docs actually matches both "cgi" and "cgi-fcgi" by just looking at the first three characters of the string ... that's why and it actually makes sense. If anything it's just to get back @hop for calling php no language for serious programmers :D
ChrisR
+1  A: 

The documenation page for php_sapi_name clearly states how it works:

"Returns a lowercase string that describes the type of interface (the Server API, SAPI) that PHP is using...."

"Although not exhaustive, the possible return values include aolserver, apache, apache2filter, apache2handler, caudium, cgi (until PHP 5.3), cgi-fcgi, cli, continuity, embed, isapi, litespeed, milter, nsapi, phttpd, pi3web, roxen, thttpd, tux, and webjames."

I'm not sure why hop doesn't think that PHP is for serious programmers (I'm a serious programmer, and I use PHP daily), but if he wants to help clarify the documentation then perhaps he can audit all possible web servers that PHP can run on and determine the names of all possible interface types for each server. Just make sure to keep that list updated as new web servers and interfaces are added.

Also, Bobby said:

"I'm intrigued as to why the doc. example inspects the first 3 characters, whilst the description states the string should be exactly "cgi""

The description for the example states:

"This example checks for the substring cgi because it may also be cgi-fcgi."

Steve
Ah - either I was being incredibly unobservant that day, or the example has been updated since I made that comment. Wholeheartedly agree with your points about PHP, though; the bashing gets VERY tiring.
Bobby Jack
A: 

upps that was wrong haha

teejay