如何定义$ _SERVER ['DOCUMENT_ROOT'] [英] How to define $_SERVER['DOCUMENT_ROOT']

查看:64
本文介绍了如何定义$ _SERVER ['DOCUMENT_ROOT']的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在运行Apache2和PHP5的Linux上安装PHP应用程序.

I am trying to install a PHP application on a Linux running Apache2 and PHP5.

我注意到,每当我尝试访问该应用程序(例如,点击 index.php 文件)时,浏览器都会返回一个空白页面.不过,运行 phpinfo()可以正常工作.

I noticed that, whenever I try to access the application (for example, hitting the index.php file), the browser returns a blank page. Running a phpinfo() works fine though.

通过测试,我意识到问题出在应用程序的代码中,它丢失了像这样的一行:

Through testing, I have realized that the problem lies in the code of the applicatio, which has lost of lines like this one:

require_once($ _ SERVER ['DOCUMENT_ROOT'].'/202-config/functions.php');

如果我将其更改为 require_once('./202-config/functions.php'); ,则应用程序可以正常运行(至少是应用程序的那一部分).

If I change this to require_once('./202-config/functions.php');, then the application runs ok (at least that part of the application.

如果我运行 print_r($ _ SERVER),则 DOCUMENT_ROOT 显示为空.

If I run a print_r($_SERVER), DOCUMENT_ROOT appears empty.

我对你们的问题是:由于我不想遍历此应用程序中的所有代码并替换这些 require_once 语句,有没有办法定义DOCUMENT_ROOT的值?

My question for you guys is: as I dont want to go through all the code in this application and replace these require_once statements, is there a way to define the value of DOCUMENT_ROOT?

谢谢.

推荐答案

来自

此数组中的条目由Web服务器创建.不能保证每个Web服务器都可以提供其中的任何一个;

The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these;

此外,如果您从命令行运行脚本,则可能无法设置.

Also if you run script from command line it might not be set.

解决方法是明确设置:

if (!isset($_SERVER['DOCUMENT_ROOT']) || empty($_SERVER['DOCUMENT_ROOT']))
    $_SERVER['DOCUMENT_ROOT'] = __DIR__; //or dirname(__FILE__) for older php verstions

注意:您使用 $ _ SERVER ['DOCUMENT_ROOT'] 包含脚本.因此,最好使用常量 __ DIR __ __ FILE __ 而不是$ _SERVER .

Note: You use $_SERVER['DOCUMENT_ROOT'] to include scripts. So it's better idea to rely on constants __DIR__ and __FILE__ rather than $_SERVER.

这篇关于如何定义$ _SERVER ['DOCUMENT_ROOT']的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆