"in php, how to detect the execution is from cli mode or through browser ? " Code Answer

2

Use the php_sapi_name() function.

if (php_sapi_name() == "cli") {
    // In cli-mode
} else {
    // Not in cli-mode
}

Here are some relevant notes from the docs:

php_sapi_name — Returns the type of interface between web server and PHP

Although not exhaustive, the possible return values include aolserver, apache, apache2filter, apache2handler, caudium, cgi (until PHP 5.3), cgi-fcgi, cli, cli-server, continuity, embed, isapi, litespeed, milter, nsapi, phttpd, pi3web, roxen, thttpd, tux, and webjames.

By aprillia.kjellstrom-a60a6b55da91 on October 14 2022

Answers related to “in php, how to detect the execution is from cli mode or through browser ? ”

Only authorized users can answer the search term. Please sign in first, or register a free account.