Skip to content

Instantly share code, notes, and snippets.

@mladoux
Created July 18, 2019 18:23
Show Gist options
  • Save mladoux/ac02d9db5a2d2f79ffdb4d9d02b3c8f3 to your computer and use it in GitHub Desktop.
Save mladoux/ac02d9db5a2d2f79ffdb4d9d02b3c8f3 to your computer and use it in GitHub Desktop.
Check if PHP is being called via command line or web browser.
<?php
function CliCheck()
{
if (defined('STDIN')) return true;
if (php_sap_name() === 'cli') return true;
if (array_key_exists('SHELL', $_ENV)) return true;
if (
empty($_SERVER['REMOTE_ADDR']) &&
! isset($_SERVER['HTTP_USER_AGENT']) &&
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