how to tell CLI or webserver execution?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Within a php file, how can you tell if it's being called by the
webserver, or by the cli?

I have some pages that I developed to be served, but now we want to
run some of them as cron jobs. I have to change some of the path
references ( our cron job scripts reference include files in the
webserver directory, and relative paths in the include files break
when they are called by a script in another directory ), and to make
everything more interoperable, I need to reference things based on
whether it's being called by Apache or by the cli. Specifically, I'm
making a reference to $_SERVER['DOCUMENT_ROOT'], which is empty when
called by the cli.

So, I need to know if the script is being called by apache or the
command line, so I can choose how to references certain included
files. How should I do this?

Re: how to tell CLI or webserver execution?

Quoted text here. Click to load it

Thanks, Phil. I just didn't know if this was the "for sure" answer.
Perhaps under certain circumstances or configurations, this might have
a value when php is called from the cli. I didn't know.

Re: how to tell CLI or webserver execution?

Quoted text here. Click to load it

Thanks, Jerry. I just wasn't sure if certain variables were more
reliable than others. I suppose SERVER_ADDR couldn't exist in the cli;
assuming it means "web sever address".

I just checked out the php manual, and it says: 'SERVER_ADDR' The IP
address of the server under which the current script is executing  ...
which doesn't necessarily rule out an ip address for the server when
called from cli... :P

Site Timeline