21

How can I import a variable from an external file? What I want to do is to have a configuration file in which I can write all my website settings and then to import these settings to every file, so I can set the website skin and things like that.

How can I do this?

palacsint
  • 28,416
  • 10
  • 82
  • 109
Adrian M.
  • 7,183
  • 15
  • 46
  • 54

4 Answers4

28

Look at this :

http://php.net/manual/en/function.parse-ini-file.php

you'll be happy :)

remi bourgarel
  • 9,231
  • 4
  • 40
  • 73
12

You can have a file with configuration and then include it on every script, like jeroen told you:

config.inc.php

$config['dbname'] = 'myDB';
$config['dbuser'] = 'user';

...

then in your scripts

include_once('config.inc.php');

You could also use inheritance where you have a model for example that uses the config and then you can extend that model class.

mandril
  • 401
  • 3
  • 10
2

It depends on how you want to store your configuration. You can just include a php file that has stuff like:

$config['stuff'] = "value";

but you can also use a config (ini) file or a xml file. PHP has standard functions available to read config files or xml files, so that´s easy as well.

jeroen
  • 91,079
  • 21
  • 114
  • 132
2

You can use auto_prepend_file to prepend your settings in every PHP scripts that is executing. It's inside the php.ini, or you can use .htaccess (php_value auto_prepend_file "path/mysettings.php"), or using ini_set(). The file must be a valid or existing.

palacsint
  • 28,416
  • 10
  • 82
  • 109
PHPWDev
  • 63
  • 1
  • 6