PHP Classes

PHP Web or Cli Detect: Detect if PHP is running from CLI or a Web server

Recommend this page to a friend!
  Info   View files Example   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2022-06-07 (26 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 43 All time: 10,364 This week: 77Up
Version License PHP version Categories
web-cli-detector 1.0.5MIT/X Consortium ...7.4PHP 5, System information, Console
Description Author

This class can detect if PHP is running from CLI or a Web server.

It can check PHP environment variables, constants, and functions to detect the environment in which PHP is running.

Currently, it can return CLI if PHP is running from a command-line interface console shell or return WEB if PHP is running from a Web server.

Innovation Award
PHP Programming Innovation award nominee
June 2022
Nominee
Vote
PHP is a flexible language that can run in different environments like, for instance, a Web server or a command-line interface shell console.

Developers can create applications that adapt to the different environments in which PHP may be running.

This package helps developers who want to adapt to the different environments PHP is running.

It checks some environment values and returns the information of the specific environment in which it is running.

Using this package, developers only need to adapt their code depending on the environment type that this package returns.

Manuel Lemos
Picture of Francisco Núñez
  Performance   Level  
Name: Francisco Núñez <contact>
Classes: 5 packages by
Country: Panama Panama
Innovation award
Innovation award
Nominee: 1x

Details

WEB-CLI-Detector

Detection of execution by command (Terminal) or web environment.

Acronym: [WEB-CLI-Detector].

Name: WEB-CLI-Detector.

Dependencies: Stand Alone / PHP v7.4.

What does [WEB-CLI-Detector] do?

is a very simple PHP [WEB-CLI-Detector] implementation that allows you to easily validate if the PHP execution is taking place from a web environment or from the execution of commands (terminal)

Why use [WEB-CLI-Detector]?

Developers need the ability to validate in which environment their libraries or applications are running, this helps decision making depending on the needs or requirements, making it easier for all developers on the same team to use the same validation.

Help to improve [WEB-CLI-Detector]?

if you want to collaborate with the development of the library; You can express your ideas or report any situation related to this in: https://github.com/arcanisgk/WEB-CLI-Detector/issues

[WEB-CLI-Detector] Configuration:

None necessary.

[WEB-CLI-Detector] Installation:

composer require arcanisgk/web-cli-detector
or 
composer require arcanisgk/web-cli-detector --dev

[WEB-CLI-Detector] Usage:

Instance of Class:


use IcarosNet\WebCLIDetector\WebCLIDetector;
require __DIR__.'\..\vendor\autoload.php';
$wc_detector = new WebCLIDetector();

Implementation of Class:


if ($wc_detector->isCLI()) {
    echo 'Running from CLI'.PHP_EOL;
}

if ($wc_detector->isWEB()) {
    echo 'Running from WEB<br>';
}

echo 'Get Raw Environment: '.$wc_detector->getEnvironment();

Other Way to implement:

this is an option to instance and validate in one line


if (WebCLIDetector::getInstance()->isCLI()) {

    //your logic

}

Example Output:

CLI Test

Image of Example Output1

Web Test

Image of Example Output2

Contributors

  • (c) 2020 - 2022 Walter Francisco Núñez Cruz icarosnet@gmail.com Donate
  Files folder image Files  
File Role Description
Files folder imageexample (1 file)
Files folder imagesrc (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  example  
File Role Description
  Accessible without login Plain text file index.php Example Example script

  Files folder image Files  /  src  
File Role Description
  Plain text file WebCLIDetector.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:43
This week:0
All time:10,364
This week:77Up
For more information send a message to info at phpclasses dot org.