1

我了解到,出于安全目的,将 mysqli 连接 config.php 文件放在根目录(公共 html 之外)。那么,如果我想在查询之前包含 config.php,那么路径是什么?

我的 config.php 如下

              <?php
              $hostname='';
              $username='';
              $password='';
              $database_name='';


             $mysqli = new mysqli($hostname, $username, $password, $database_name); 
             ?>

在我的其他 php 文件中

             <?include('config.php');?>

我的 config.php 在根目录中。include 语句中的路径是什么?

4

2 回答 2

3

When you say "root directory" that usually means the root directory of a filesystem. On most *nix (Linux, FreeBSD, Unix) installs, that means your filesystem path is simply "/". That is, if your config file is there, your PHP file should look like this:

<?php
    include('/config.php');
?>

However, it would generally be both a bad idea and would require superuser privileges to actually put a file at the filesystem root of "/". So it's likely (but we have to guess), that you mean the DOCUMENT ROOT for your web server. Most web servers running PHP make that value available via the super-global $_SERVER['DOCUMENT_ROOT'] as mentioned by Kai Qing in his comment. If that is where your file is, then you could put this in your PHP file:

<?php
    include($_SERVER['DOCUMENT_ROOT'] . '/config.php');
?>

See also http://www.php.net/manual/en/reserved.variables.server.php .

于 2013-06-14T22:26:09.250 回答
1

You can try this.

include($_SERVER['DOCUMENT_ROOT'].'/config.php')

http://css-tricks.com/php-include-from-root/

This link has another technique as well, if your DOCUMENT_ROOT is not set properly.

于 2013-06-14T22:26:19.107 回答