0

I have this ini file: /etc/php5/apache2/conf.d/appconfig.ini

Here is the content of the file:

serverhost = localhost

And I reset the apache2 server. How do I get this value from a PHP script without specifying path to ini file? I assumed when PHP parsed this file, it would store the value for serverhost somewhere accessible. I know that this file was parsed because I ran php --ini.

I tried this script:

<?php
echo ini_get("serverhost");
?>

But nothing was returned. I can get it this way:

<?php
$file = parse_ini_file("/etc/php5/apache2/conf.d/appconfig.ini");
echo $file["serverhost"];
?>

But I want to know how to get it as an environment variable of sort. PHP did parse the file right?

After all, I will store a password in that appconfig.ini. I wonder what is best practice for storing password.

Nam Thai
  • 841
  • 1
  • 10
  • 26
  • Why not set it in your .htaccess file? – Twisty Apr 20 '15 at 23:13
  • What does `echo('
    '.print_r($_SERVER,1).'
    ');` display?
    – user1032531 Apr 20 '15 at 23:13
  • `php_value serverhost localhost`http://php.net/manual/en/configuration.changes.php – Twisty Apr 20 '15 at 23:16
  • @Twisty where can I find .htaccess file? Another reason is that I would prefer to keep different config file for different application. When I add them to `php.ini`, I can retrieve it. But not when I try to get values from files in `conf.d` folder – Nam Thai Apr 20 '15 at 23:16
  • @Twisty I tried that as well that doesn't work – Nam Thai Apr 20 '15 at 23:17
  • You would create the .htaccess file or find it in your sites folder. Since it is a dot file, it will be hidden to your FTP Client or to `ls`. You must show hidden files. I would advise creating unique include files that use `ini_set()` if needed. That way you can just include the proper file for a specific app. – Twisty Apr 20 '15 at 23:21
  • @user1032531 there are information in there that I don't wish to share. Which tag are you looking for exactly? – Nam Thai Apr 20 '15 at 23:21
  • `ini_get` is only for getting pre-defined settings from php's `php.ini` file. You would either need to specify the file or set an environment variable from apache for use in php like: http://stackoverflow.com/questions/10902433/setting-environment-variables-for-accessing-in-php – Jonathan Kuhn Apr 20 '15 at 23:53
  • also, if you just want to get the hostname for the loaded site, `$_SERVER['HTTP_HOST']` already has it. – Jonathan Kuhn Apr 20 '15 at 23:56
  • @Twisty @Jonathan Kuhn Alright the real reason I'm using that .ini file is because I will store a password there. I have heard that I should store this file outside of root directory, that's why I wasn't sure if I should use .htaccess and keep it in the same folder. Is it safe to use either `$file = parse_ini_file("/etc/php5/apache2/conf.d/appconfig.ini");` or `.htaccess` ? – Nam Thai Apr 21 '15 at 00:06
  • store the details within an include file that is in the parent folder or another folder outside your wwwroot folder. That way you can just call `include_once '../appconfig.inc.php';` and you can use `DECLARE` or assign a variable like `$serverhost = 'localhost';` within the include. It will be safe and a lot easier to use. – Twisty Apr 21 '15 at 04:41

1 Answers1

0

To protect connection setting for a mail server or SQL Server, you can store these in a file outside of your wwwroot folder. For example, if your site resides on the server at /var/sites/mysite/www you would place the file within a directory like /var/sites/mysite/_conf. For example, create a file at /var/sites/mysite/_conf/appconfig.inc.php with:

<?php
$smtpServerHost = 'localhost';
$smtpServerUser = 'user@example.com';
$smtpServerPass = 'Password1';
?>

Then when you include the file in your app, you can simply call up those variables. For example, if you had formhandler.php:

<?php

include_once '../_conf/appconfig.inc.php';
include_ once 'Mail.php';

$smtp = Mail::factory('smtp', array (
  'host' => $smtpServerHost,
  'auth' => true,
  'username' => $smtpServerUser,
  'password' => $smtpServerPassword)
);

$headers['From']    = 'richard@example.com';
$headers['To']      = 'joe@example.com';
$headers['Subject'] = 'Test message';

$mail = $smtp->send($to, $headers, print_r($_POST));

?>
Twisty
  • 30,304
  • 2
  • 26
  • 45