PHP include()不加载config.php

时间:2012-08-29 22:41:02

标签: php

我正在尝试创建一个简单的脚本,但是当我尝试包含配置文件时,它不会加载。 这是我的代码 -

<?php
$config = include("http://MyWebSite/config.php");
$mysql = mysql_connect($host, $user, $pass);
if (!$mysql) { die(mysql_error()); }
$data = mysql_select_db($database);
if (!$data) { die(mysql_error()); }
?>

4 个答案:

答案 0 :(得分:5)

试试这个:

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

另外,不要将其分配给变量。您只想在脚本中包含该文件。

答案 1 :(得分:1)

你应该尝试那样

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

因为在正常的服务器设置中,您不允许直接包含远程文件。

答案 2 :(得分:1)

1)包含不返回任何内容 2)包含整个URL必须由服务器启用

答案 3 :(得分:0)

您需要确保文件的路径正确。以下代码将在与您运行的脚本相同的目录中查找config.php:

include('config.php');

如果这不起作用,您可以查看错误日志或使用以下代码打开错误报告:

ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL);
include('config.php');

现在,错误消息应该显示它在哪里查找文件。错误将类似于以下内容:

Warning: include() [function.include]: Failed opening 'config.php' for inclusion (include_path='.:/usr/share/pear:/usr/share/php') in /path/to/your/script.php on line 4

以上消息表明它无法在以下任何位置找到config.php:

 /path/to/your/ 
 /usr/share/pear/
 /usr/share/php/