How to determine if a PHP file is loaded via cron/command line

前端 未结 4 537
长情又很酷
长情又很酷 2020-12-06 02:45

I need to determine whether the PHP file is being loaded via cron or command line within the code. How can I do this?

4条回答
  •  再見小時候
    2020-12-06 03:20

    This is one simple way. Certain elements of the $_SERVER array are only set if called from HTTP. Thus you can:

    if(!isset($_SERVER['REQUEST_METHOD'])){
     // from cron or command line
    }else{
     // from HTTP
    }
    

    Others include: $_SERVER['HTTP_HOST']

提交回复
热议问题