Detect whether PHP is CLI or Apache

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Detect whether PHP is CLI or Apache

Post by alex.barylski »

I know it's probably obtainable with some weird checks...using phpinfo() for example but I'm curious incase there is an API which I have overlooked...

Is it possible to detemrine whether PHP instance running is invoked by Apache or CLI?

Will I have to detect this informaiton much like a browser is detected, by checking environment variables, etc.

Hopefully there is a simple API :D
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Re: Detect whether PHP is CLI or Apache

Post by Cirdan »

There is an environment variable named SERVER_SOFTWARE, probably access it through $_SEVER[]
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Detect whether PHP is CLI or Apache

Post by Christopher »

$_SEVER["SERVER_SOFTWARE"] probably will not be defined in CLI -- along with many other values. I would check something like isset($_SERVER["SERVER_PROTOCOL"]) and equal "HTTP" to see if it's a webserver and isset($_SERVER["argc"]) to see if it's CLI.
(#10850)
Post Reply