Personally I'd have a look at $_SERVER['SERVER_SOFTWARE'] or something similar, which will always be set if you're invoked from the web, and never set if run from the CLI.
I've had a requirement to do this in the past, and just wrote a function IsInWeb()
Mark