开发者

PHP $_SERVER['DOCUMENT_ROOT'] is empty when called from a Cron Tab

When I use the following code:

<?
print_r($_SERVER);
?>

I get a nice list off all server variables. However, when I enter the same code i开发者_运维知识库n 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');

Now use PUBLIC_PATH instead of $_SERVER['DOCUMENT_ROOT']


When you execute via cron, are you just calling php myscript.php or are you accessing the php script via http? If you're executing directly via http then the apache variables (such as DocumentRoot) will not be available.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜