php - 检查您的 PHP 是由 Cron 作业还是 HTTP 请求运行的最正确方法是什么?

标签 php http cron request

我现在正在做这样的事情:

if (empty($_SERVER['SERVER_NAME'])) {
  // the script is run by a Cron job
} else {
  // the script is run by a HTTP request
}

会不会在某些情况下出错?更具体地说,$_SERVER['SERVER_NAME'] 总是由 Apache 设置吗?我知道 $_SERVER 中的一些变量,例如 $_SERVER['REMOTE_ADDR'] 可能会被客户端污染。

有什么建议吗?

最佳答案

制作两个单独的脚本是我的第一个建议。

此外,$_SERVER['SERVER_NAME'] 取决于服务器设置,但应始终设置。

来自 Apache 文档:

If no ServerName is specified, then the server attempts to deduce the hostname by performing a reverse lookup on the IP address. If no port is specified in the ServerName, then the server will use the port from the incoming request. For optimal reliability and predictability, you should specify an explicit hostname and port using the ServerName directive.

关于php - 检查您的 PHP 是由 Cron 作业还是 HTTP 请求运行的最正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9514799/

相关文章:

php - mysql/php 中事件的基于时间的资源可用性

php - Jira REST API 的文件附件返回空数组

javascript - 缺少 CORS header 'Access-Control-Allow-Origin'

php - 每秒自动数据库读/写

php - 帮助 Kohana 3 和 CRON

php - 如何从 pg_insert() 获取错误信息?

http - HTTP 请求/响应 header 的平均大小是多少?

java - 低于 19 的较低 API 上的 StandardCharsets.UTF_8

linux - crontab 路径和用户

php - 如何使用 PHP 从两个不同的表中进行选择,从结果中创建变量并对它们进行数学运算