How to check with PHP if the script is being run from the console or browser request?

后端 未结 6 1535
孤街浪徒
孤街浪徒 2021-02-02 05:58

I tried things like $_ENV[\'CLIENTNAME\'] == \'Console\' but that seems to work on only certain OS\'s (worked in windows, not linux).

I tried !empty($_ENV[\'SHELL\']) b

6条回答
  •  遇见更好的自我
    2021-02-02 06:42

    if ($argc > 0) {
        // Command line was used
    } else {
        // Browser was used
    }
    

    $argc coounts the amount of arguments passed to the command line. Simply using php page.php, $argc will return 1

    Calling page.php with a browser, $argc will return NULL

提交回复
热议问题