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 - 如何每天自动运行一个php脚本?

php - 如何查看 cron 作业中返回的 php 错误

php - Android GCM 消息已成功发送但未收到

php - 发送文件 POST C++

c# - 如何在 C# 中将 HTTP Last-Modified header 从 UTC 解析为 DateTime

php - include_once函数是否创建了http请求

php:如何使用准备好的语句从 sql 获取用户名或电子邮件

php - 如何在数据库中插入带有双引号的值?

javascript - 如何在ajax代码中访问javascript变量值

python - 运行带输出和不带输出的 python 脚本