Skip to content

Instantly share code, notes, and snippets.

@Bugfighter
Created April 26, 2024 21:10
Show Gist options
  • Save Bugfighter/db4ea307c3a54d2bad9da31eaad7ee0e to your computer and use it in GitHub Desktop.
Save Bugfighter/db4ea307c3a54d2bad9da31eaad7ee0e to your computer and use it in GitHub Desktop.
Check for CLI script call.
<?php
/**
* This function checks on various environments for script call via CLI.
**/
function is_cli()
{
if ( defined('STDIN') )
{
return true;
}
if ( php_sapi_name() === 'cli' )
{
return true;
}
if ( array_key_exists('SHELL', $_ENV) ) {
return true;
}
if ( empty($_SERVER['REMOTE_ADDR']) and !isset($_SERVER['HTTP_USER_AGENT']) and count($_SERVER['argv']) > 0)
{
return true;
}
if ( !array_key_exists('REQUEST_METHOD', $_SERVER) )
{
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment