【发布时间】:2013-03-18 22:19:28
【问题描述】:
我有一个网站,它连接到一个 mysql 数据库,它使用一些其他密码,这些密码在位于 /home/user/public_html/inc/instance-config.php 的文件中定义。
为了安全起见,我想将我的密码保存在 /public_html 目录之上。基本上我创建了一个名为 /home/user/secrets.php 的文件,主配置文件应该需要这个文件并从中获取密码。 但事实并非如此。
我的文件 /home/user/public_html/inc/instance-config.php 中的这段代码没有错误:
$secrets = str_replace ( 'public_html', '', getcwd() ) . 'secrets.php';
if ( file_exists( $secrets ) ){
include $secrets ;
}
**// confirm that the values where imported**
moo( 'The secret value is ' . $secret_value );
$config['db']['type'] = 'mysql';
$config['db']['server'] = 'localhost';
$config['db']['database'] = 'xxxxxxx';
$config['db']['user'] = 'xxxxxxx';
$config['db']['password'] = 'xxxxxxx';
$config['cookies']['mod'] = 'xxxxxxxx';
$config['cookies']['salt'] = 'xxxxxx';
我的文件 secrets.php 的最后七行与配置和密码相同,还有一个名为“secret_value”的变量,我基本上用它来检查文件已正确包含。所以,我这样做了,因为我的“moo”函数输出到一个日志文件,我得到了类似的东西:
03/28/2013 07:36:52 am -- The secret value is Everything OK!
03/28/2013 07:36:55 am -- The secret value is Everything OK!
03/28/2013 07:36:55 am -- The secret value is Everything OK!
03/28/2013 06:36:56 am -- The secret value is Everything OK!
03/28/2013 07:36:57 am -- The secret value is Everything OK!
03/28/2013 06:36:57 am -- Rebuilt page 1 from ALL
03/28/2013 07:36:57 am -- The secret value is Everything OK!
03/28/2013 06:36:58 am -- The secret value is Everything OK!
03/28/2013 06:36:58 am -- The secret value is Everything OK!
03/28/2013 06:36:58 am -- The secret value is Everything OK!
03/28/2013 06:36:58 am -- The secret value is Everything OK!
03/28/2013 06:36:58 am -- The secret value is Everything OK!
03/28/2013 06:36:58 am -- The secret value is Everything OK!
03/28/2013 06:36:58 am -- The secret value is Everything OK!
03/28/2013 06:36:58 am -- The secret value is Everything OK!
03/28/2013 06:36:58 am -- The secret value is Everything OK!
03/28/2013 06:36:59 am -- The secret value is Everything OK!
03/28/2013 06:36:59 am -- The secret value is Everything OK!
03/28/2013 06:36:59 am -- The secret value is Everything OK!
03/28/2013 06:36:59 am -- The secret value is Everything OK!
03/28/2013 06:36:59 am -- The secret value is Everything OK!
03/28/2013 06:36:59 am -- The secret value is Everything OK!
03/28/2013 06:36:59 am -- The secret value is Everything OK!
03/28/2013 06:36:59 am -- The secret value is Everything OK!
03/28/2013 06:36:59 am -- The secret value is Everything OK!
03/28/2013 07:36:59 am -- The secret value is Everything OK!
因此,似乎正确地需要该文件。我试图从我的实例配置文件中删除这些行,以便它们只出现在 secret.php 文件中。但首先,我尝试分支代码,检查文件是否存在。如果文件存在,那么它应该从中获取配置变量。这是我的代码:
moo(':: GET READY');
if ( file_exists( $secrets ) ){
require $secrets ;
if ( isset ($secret_value) )
moo( 'The secret value is ' . $secret_value);
else
moo('Required but could not find the secret value');
}else{
$config['db']['type'] = 'mysql';
$config['db']['server'] = 'localhost';
$config['db']['database'] = 'xxxxxx';
$config['db']['user'] = 'xxxxxxxx';
$config['db']['password'] = 'xxxxxxxxxx';
$config['cookies']['mod'] = 'xxxxxxxxxxxxx';
$config['cookies']['salt'] = 'xxxxxxxxx';
moo( 'The secret value was not obtained');
}
哦,是的,我得到了一个输出行列表,例如:
03/28/2013 07:41:07 am -- :: GET READY ::
03/28/2013 07:41:07 am -- The secret value is Everything OK!
每个“GET READY”后面都跟着相应的“Everything is OK!”。在日志文件中,我没有看到“未获得秘密值”或“需要但找不到...”行。
但是:
在浏览器中,我收到 Error 500 消息并且页面停止加载。为什么,哦,为什么?我检查了 Internet,大多数人说这可能是 PHP 找不到您的文件的问题。我试图帮助代码尽可能多地找到文件:
// this adds /home/user to the include path, because secret.php
// is located at /home/user/secret.php
if ( false == strpos( ini_get('include_path'), exec('echo ~') ) ) {
ini_set('include_path', ini_get('include_path') . ':' . exec('echo ~') );
}
// log errors to see if we can find the cause of the problem
ini_set('log_errors', 1);
ini_set('error_log', '/home/user/errors.log');
ini_set('error_reporting', E_ALL);
// get absolute path of the file
$secrets = str_replace ( 'public_html', '', getcwd() ) . 'secreto.php';
没有任何效果,总是同样的 500 错误。我能做些什么?我几乎什么都试过了。正如我在所有执行过程中得到的输出所示,该文件的位置似乎正确。但是服务器给了我一个错误 500 页面。
有什么想法吗?
【问题讨论】:
-
您是否也检查过服务器(Apache?)错误日志文件?
-
附带说明
file_exists()并不可靠,因为如果您传递目录名称,它将返回 true。最好使用is_file()。 -
但是代码找到文件的一部分“secret_value”的值,这是否足以证明文件被正确包含?为什么是 500?
-
尝试在文件中首先设置 ini_set 值以记录错误。我的猜测是问题出在 exec() 函数上。 exec('echo ~') 的目的是什么?
-
exec('echo ~') 显示用户的主目录,在本例中为 /home/user,即 secret.php 文件所在的位置。我试过直接在 require 函数中设置文件名,如 require('/home/user/secret.php');但这并不能解决问题。带有代码的文件包含在其他文件中,这可能是个问题吗?就像这样:functions.php 调用一个需要instance-config.php 的函数,而instance-config 需要secret.php 文件。需要的文件中可以有 require 调用吗?