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   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 87 All time: 9,999 This week: 571Up
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
Number 8
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: 6 packages by
Country: Panama Panama
Age: 38
All time rank: 36773 in Panama Panama
Week rank: 206 Up1 in Panama Panama Up
Innovation award
Innovation award
Nominee: 2x

Example

<?php

declare(strict_types=1);

use
IcarosNet\WebCLIDetector\WebCLIDetector;

require_once
__DIR__."/../vendor/autoload.php";

$wc_detector = new WebCLIDetector();

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();


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 (6)  
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 (6)  /  example  
File Role Description
  Accessible without login Plain text file index.php Example Example script

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

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:87
This week:0
All time:9,999
This week:571Up
User Comments (1)