PHP $_SERVER['DOCUMENT_ROOT']

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13394924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:19:59  来源:igfitidea点击:

PHP $_SERVER['DOCUMENT_ROOT']

php

提问by Boon

        $username;
        $welcomeMessage;
        if( isset( $_SESSION['username'] ) ){
            $username = $_SESSION['username'];
            $welcomeMessage = "Hello $username! | ";
            $welcomeMessage .= '<a href="'.$_SERVER['DOCUMENT_ROOT'].'/nmc/Admin/LogoutProcessor.php">Logout</a>';
        } else {
            $welcomeMessage = "Welcome | ";
            $welcomeMessage .= '<a href="'.$_SERVER['DOCUMENT_ROOT'].'/nmc/Admin/LoginPage.php">Login</a>';
        }

The code above returns "file:///C:/xampp/htdocs/nmc/Admin/LoginPage.php"

上面的代码返回“file:///C:/xampp/htdocs/nmc/Admin/LoginPage.php”

I am using xampp to develop a website, basically uses its own server processing, therefore I can't use the above link. I have several webpages in different directories which have to link to the LoginPage.php, and I need a standard link. The above code is in a Class where other pages in different directories can call it.

我是用xampp开发网站,基本都是用自己的服务器处理,所以不能用上面的链接。我在不同目录中有几个网页,它们必须链接到 LoginPage.php,我需要一个标准链接。上面的代码在一个Class中,不同目录下的其他页面可以调用它。

Can anyone tell me how to resolve this problem?

谁能告诉我如何解决这个问题?

Thanks!

谢谢!

回答by banzsh

$_SERVER['DOCUMENT_ROOT'] returns

$_SERVER['DOCUMENT_ROOT'] 返回

The document root directory under which the current script is executing, as defined in the server's configuration file.

在服务器的配置文件中定义的当前脚本正在执行的文档根目录。

You could use $_SERVER['HTTP_HOST']or absolute paths like <a href="/nmc/Admin/LoginPage.php">Login</a>

您可以使用$_SERVER['HTTP_HOST']或绝对路径,如<a href="/nmc/Admin/LoginPage.php">Login</a>

回答by Abhishek Saha

In such kind of scenarios, its always better to create a config.php file and save it in your root directory. In the config file you define few parameters.

在这种情况下,最好创建一个 config.php 文件并将其保存在您的根目录中。在配置文件中,您定义了几个参数。

Call this config file in every page. Your config file can be similar to one below.

在每个页面中调用此配置文件。您的配置文件可以类似于以下之一。

define('APP_NAME',"beta");  
define('HTTP_SERVER', 'http://localhost/'); 
define('SITE_NAME', 'http://localhost/');   
define('DOCUMENT_ROOT',$_SERVER['DOCUMENT_ROOT'].APP_NAME); 

You can also define your directory for images, css, etc. which you think will be used in multiple places.

您还可以为您认为将在多个地方使用的图像、CSS 等定义您的目录。

So instead of

所以代替

$welcomeMessage .= '<a href="'.$_SERVER['DOCUMENT_ROOT'].'/nmc/Admin/LogoutProcessor.php">Logout</a>';

you can write,

你可以写,

$welcomeMessage .= '<a href="'.HTTP_SERVER.'/nmc/Admin/LogoutProcessor.php">Logout</a>';

回答by alesdario

 $_SERVER['DOCUMENT_ROOT']

returns a physical file system path. It's not an HTTP URL.

返回物理文件系统路径。它不是 HTTP URL。

Try removing it to use an absolute URL path:

尝试删除它以使用绝对 URL 路径:

 <a href="/nmc/Admin/LoginPage.php">Login</a>

or try using $_SERVER['HTTP_HOST'] instead.

或者尝试使用 $_SERVER['HTTP_HOST'] 代替。

BTW, read $_SERVER documentation.

顺便说一句,阅读 $_SERVER 文档

回答by Marcos Cassiano

If you're using Wamp Server or similar, "/" is the document root of the "www" folder, so you have to write "/yoursitesfoldername/nmc/Admin/LogoutProcessor.php".

如果您使用的是 Wamp Server 或类似产品,“ /”是“www”文件夹的文档根目录,因此您必须编写"/yoursitesfoldername/nmc/Admin/LogoutProcessor.php".

回答by Evan Siroky

I inherited a site that used this everywhere, so when I went to test it with xampp, I ultimately created a virtualhost to load the site.

我继承了一个到处都使用它的站点,所以当我用 xampp 测试它时,我最终创建了一个虚拟主机来加载站点。

See this article for details: http://www.dreamincode.net/forums/topic/307265-change-serverdocument-root-path-for-xammp-light/

详情请看这篇文章:http: //www.dreamincode.net/forums/topic/307265-change-serverdocument-root-path-for-xammp-light/

Here is my httpd-vhosts.conffile:

这是我的httpd-vhosts.conf文件:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot "C:/xampp/htdocs/examplesite/nested/path/to/content"
    ServerName example.localhost
    ErrorLog "logs/example.localhost-error.log"
    CustomLog "logs/example.localhost-access.log" common
</VirtualHost>