views:

255

answers:

2

I tried things like $_ENV['CLIENTNAME'] == 'Console' but that seems to work on only certain OS's (worked in windows, not linux).

I tried !empty($_ENV['SHELL']) but that doesn't work always either...

Is there a way to check this that will work in all OS's/environments?

Thanks!

+1  A: 

Check the HTTP_USER_AGENT , it should exist in http request

J-16 SDiZ
There's all sorts of variables in $_SERVER that work under the same idea.
Matthew Scharley
User agent is optional. I wouldn’t rely on it, since there’s much better solution
Maciej Łebkowski
+5  A: 

Use php_sapi_name()

Returns a lowercase string that describes the type of interface (the Server API, SAPI) that PHP is using. For example, in CLI PHP this string will be "cli" whereas with Apache it may have several different values depending on the exact SAPI used.

For example:

$isCLI = ( php_sapi_name() == 'cli' );

You can also use the constant PHP_SAPI

Tom Haigh