我有一个网站,它连接到一个 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!”。在日志文件中,我没有看到“未获得秘密值”或“需要但找不到...”行。
但是:
在浏览器中,我收到一条错误 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 页面。
有任何想法吗?