0

我有一些require_once()问题。

代码:

<?php 
$serverName = $_SERVER['SERVER_NAME'];

if ($serverName != 'xxx.xxx.xxx.xxx') {
    require_once($_SERVER['DOCUMENT_ROOT'] . "/config.php");
    } else {
    require_once($_SERVER['SERVER_NAME'] . "/config.php");
    }
?>

错误/警告:

Warning: require_once() [function.require-once]: Unable to accesss xxx.xxx.xxx.xxx/config.php in /var/www/vhosts/site.com/httpdocs/inc/header.php on line 7


Warning: require_once(xxx.xxx.xxx.xxx/config.php) [function.require-once]: failed to open stream: No such file or directory in /var/www/vhosts/site.com/httpdocs/inc/header.php on line 7


Fatal error: require_once() [function.require]: Failed opening required 'xxx.xxx.xxx.xxx/config.php' (include_path='.:') in /var/www/vhosts/site.com/httpdocs/inc/header.php on line 7

config.php文件正在我的header.php文件中调用。一切都在本地运行良好,但是一旦它在实时服务器上,我就会得到上面的错误。

不确定这是否重要,但我通过 IP 地址访问它,因为它是一个开发站点。

是的,这些config.php文件确实存在于根目录中。

有任何想法吗?

4

2 回答 2

0

require_once 需要脚本的绝对路径,但 $_SERVER['SERVER_NAME'] 给出 url 而不是路径,因此无法加载。

require_once($_SERVER['SERVER_NAME'] . "/config.php");
于 2012-02-28T01:11:55.563 回答
0

您最好使用相对路径访问配置文件:

<?php 
//header.php
$serverName = $_SERVER['SERVER_NAME'];

if ($serverName == 'localhost' || $serverName == '127.0.0.1') {
    require_once("./local_config.php");
}else{
    require_once("./config.php");
}
?>
于 2012-02-28T01:27:02.233 回答