How do I Determine if a PHP Script is Running from the CLI or a Web Server?

DDD
Release: 2024-10-28 09:59:02
Original
111 people have browsed it

How do I Determine if a PHP Script is Running from the CLI or a Web Server?

Determining PHP Invocation Source: CLI vs Web Server

Determining whether a PHP script is executed from the command-line interface (CLI) or a web server, such as Apache with mod_php, is essential for managing your scripts accordingly.

To effectively assess the invocation source, utilize the php_sapi_name() function. It returns a lowercase string specifying the interface type. Additionally, consider using the PHP_SAPI constant for this purpose.

For instance, to discern if PHP operates within the CLI, employ the following function:

<code class="php">function isCommandLineInterface()
{
    return (php_sapi_name() === 'cli');
}</code>
Copy after login

This approach provides a reliable method to determine the origin of your PHP script, allowing you to adapt its execution appropriately.

The above is the detailed content of How do I Determine if a PHP Script is Running from the CLI or a Web Server?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!