从Cron选项卡调用时,PHP $ _SERVER ['DOCUMENT_ROOT']为空 [英] PHP $_SERVER['DOCUMENT_ROOT'] is empty when called from a Cron Tab
问题描述
当我使用以下代码时:
<?
print_r($_SERVER);
?>
我从所有服务器变量中得到了一个不错的列表.但是,当我在脚本中输入相同的代码(即由Cron Tab调用)时,没有列出很多变量.另外,Document_root变量没有值.
I get a nice list off all server variables. However, when I enter the same code in a script, that is being called by a Cron Tab, a lot of variables are not listed. Also, the Document_root variable has no value.
仅在移至其他服务器后才遇到此问题.我需要调整一些服务器设置来避免这种情况吗?
I only have had this issue after moving to a different server. Do I need to tweak some server settings to avoid this?
推荐答案
最好不要依赖此变量,因为它不一定总是设置(就像您发现的那样).
It's best not to rely on this variable as it isn't always set (just as you discovered).
尝试设置并使用如下常量:
Try setting and using a constant like this instead:
define('PUBLIC_PATH', '/var/www/path/to/public');
// OR something like:
define('PUBLIC_PATH', dirname(__DIR__) . '/public');
现在使用PUBLIC_PATH
代替$_SERVER['DOCUMENT_ROOT']
这篇关于从Cron选项卡调用时,PHP $ _SERVER ['DOCUMENT_ROOT']为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!