I have a website and it connects to a mysql database and it uses some other passwords that are defined in a file located in /home/user/public_html/inc/instance-config.php.
I want to keep my passwords above the /public_html directory for security. Basically I made a file called /home/user/secrets.php and the main config file should require this file and get the passwords from it. But it doesn't.
This code in my file /home/user/public_html/inc/instance-config.php gives no errors:
$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';
My file secrets.php has the same last seven lines with the configuration and passwords, and a variable called "secret_value" which I use basically to check that the file has been included correctly. So, I do this, and since my "moo" function outputs to a log file, I get something like:
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!
So, it seems that the file is being required correctly. I tried to delete those lines from my instance-config file, so that they only appear in the secret.php file. But first, I tried to branch the code, checking for the existence of the file. If the file exists, then it should get the configuration variables from it. This is my code:
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');
}
And oh yes, I get a list of output lines like:
03/28/2013 07:41:07 am -- :: GET READY ::
03/28/2013 07:41:07 am -- The secret value is Everything OK!
And every "GET READY" is followed by the correspondent "Everything is OK!". Nowhere in the log file i see a "The secret value was not obtained" or "Required but could not find..." line.
However:
In the browser, I get an Error 500 message and the page stops loading. Why, oh why? I've checked the Internet, and most people say it could be a problem with PHP not finding your file. I've tried to help the code locate the file as much as I can:
// 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';
And nothing works, always the same 500 error. What can I do? I've tried almost everything. The file seems to be located properly, as indicated by the output that I got during all the executions. Yet the server gives me an Error 500 page.
Any ideas?
Did you see what is the php error? Error 500 is webserver/apache says php has a problem. The problem itself most likely is in webserver/apache error_log file.
Alternatively, run your php file from command line(php -l somefile.php can do syntax check) and php would tell you the problem (syntax or runtime), run the file from the same dir you are hosting it like php somefile.php.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With