views:

37

answers:

3

I have a PHP script on a webserver.

This file is invoked via the shell by another program but it could still be run by the webserver in response to an HTTP request.

How can the script determine the way it was invoked?

+3  A: 

There are lots of ways; I check if $_SERVER['HTTP_HOST'] is empty. I think the technically correct way is to see if php_sapi_name() returns cli

Michael Mrozek
Ya, that's probably the best way.
George Edison
A: 

If it is executed from the shell then it won't have HTTP headers because it wasn't requested from HTTP protocols.

animuson
A: 

There are certain environmental variables you can check. for example $_SERVER["REQUEST_METHOD"]

if (isset($_SERVER["REQUEST_METHOD"]))
   // run by server

good luck

Jared Forsyth