how to make a PHP file run-able only through CLI mode?

匆匆过客 提交于 2019-11-30 12:09:30

Another trick, $_SERVER has variables that are only set in CLI mode.

Here is what i'm using, for a long time now... (since php 4 iirc)

(PHP_SAPI !== 'cli' || isset($_SERVER['HTTP_USER_AGENT'])) && die('cli only');

to be used as first line of the php script.

Here is a trick:

You can check for the $argc/$argv parameters which are always available in CLI mode.

#!/usr/bin/php
<?php 

ini_set('register_argc_argv', 0);  

if (!isset($argc) || is_null($argc))
{ 
    echo 'Not CLI mode';
} else {
    echo 'CLI mode';
}

register_argc_argv

$argc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!