2012-02-28 65 views
0

我有一些require_once()问题。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文件确实存在于根目录中。

任何想法?

+0

您刚才'回声$ _ SERVER ['SERVER_NAME]'看你到达那里呢? – 2012-02-28 01:11:08

+0

由于安全原因您无法通过ip地址访问php文件 – 2012-02-28 01:13:18

+0

@Michael - 是的,它返回IP地址 – 2012-02-28 01:18:13

回答

0

require_once需要脚本的绝对路径,但$ _ SERVER [“SERVER_NAME”] 给予网址,而不是路径,因此无法加载。

require_once($_SERVER['SERVER_NAME'] . "/config.php"); 
+1

'SERVER_NAME'包含主机名,而不是URL,因此类似于'example.com/config.php'的路径对于使用服务器名称作为目录的共享主机或虚拟主机配置是有效的... – 2012-02-28 01:14:02

+0

require_once将接受绝对OR相对路径,并与allow_url_include启用,也将加载远程资源(适用于include,include_once,require,require_once) – 2012-02-28 01:17:11

+0

@迈克尔好吧,这不是OP的情况下,他需要使用'$ _SERVER ['SERVER_NAME'] 。 “../../ config.php”'当他不如使用'./config.php'时 – 2012-02-28 01:23:20

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"); 
} 
?> 
+0

没有那个运气。猜猜我应该打电话给我的主机... – 2012-02-28 01:30:08

+0

@Paul你仍然得到相同的错误,你应该打印出'$ _SERVER ['SERVER_NAME']',看看它是否设置或使用include()'而不是较慢'require_once()' – 2012-02-28 01:34:29

+0

是的,设置了'$ _SERVER ['SERVER_NAME']'。我刚刚接到了我的托管公司的电话,它看起来像我的服务器不包含尾部的'/',但是我的本地机器。 – 2012-02-28 01:47:18