Home arrow PHP arrow Page 7 - Configuration Manipulation With PHP Config

Changing Things Around - PHP

Tired of writing (and rewriting) code to manage your application's configuration variables? Take a look at the PEAR Config class, a PHP toolkit designed specifically for manipulating configuration files and the data within them. This article demonstrates using the Config class to read and write configuration files in XML, PHP and INI formats, and use built-in methods to easily build Web-based application configuration modules.

  1. Configuration Manipulation With PHP Config
  2. Plug And Play
  3. Your Friendly Neighbourhood Hulk
  4. Different Strokes
  5. Array Of Hope
  6. Up A Tree
  7. Changing Things Around
  8. Giving Birth
  9. Link Zone
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 66
September 04, 2003

print this article



Just as you can getContent(), you can also setContent() - and this next example demonstrates how, by using this method to change a configuration variable read from a file.


// include class

// instantiate object
$c = new Config();

// read configuration data and get reference to root
$root =& $c->parseConfig("mail.ini", "IniFile");

// get reference to [Settings] section of file
$settingsSection =& $root->getItem("section", "Settings");

// get POP account
$popAccountDirective =& $settingsSection->getItem("directive", "POPAccount"); $popAccountDirective->setContent("my.new.email.address");

// write configuration back to file


In this case, the setContent() method is used to change the value of a configuration variable, and then the writeConfig() method is used to write the entire set of variables back to the file.

This is one of the more common applications of the Config class - displaying the current configuration to the user, and saving modifications back - so pay attention to this next example, which builds on what you've just learned to create a script accepting user input for application configuration. This script is divided into two parts: a form which displays the current configuration (if available) and allows the user to edit it, and a form processor, which accepts the new configuration and saves it to a file.




if (!file_exists("ppp.conf"))
echo "Configuration not found!";

// include class

// instantiate object
$c = new Config();

// read config file
// get root
$root =& $c->parseConfig("ppp.conf", "IniFile");

// get sections
$pppSection =& $root->getItem("section", "ppp");
$userSection =& $root->getItem("section", "account");

// get directives
$numberDirective =& $pppSection->getItem("directive", "number");
$retriesDirective =& $pppSection->getItem("directive", "retries");
$deviceDirective =& $pppSection->getItem("directive", "device");
$connect_scriptDirective =& $pppSection->getItem("directive", "connect_script");
$ppp_userDirective =& $userSection->getItem("directive", "ppp_user");
$ppp_passDirective =& $userSection->getItem("directive", "ppp_pass");

// form not yet submitted
if (!$_POST['submit'])
// prefill form with values from file
<table border="0" cellspacing="5" cellpadding="5">
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">

<td>ISP user name</td>
<input type="text" name="user"

<td>ISP user password</td>
<input type="text" name="pass"

<td>ISP dial-up number</td>
<input type="text" name="number"

<td>Number of retries</td>
<td><input type="text" name="retries"
value="<?=$retriesDirective->getContent();?>" size="4"></td> </tr>

<td>Connection device</td>
<input type="text" name="device"

<td>Connection script</td>
<input type="text" name="script"

<td colspan="2" align="center"><input type="submit" name="submit" value="Save Configuration"></td> </tr>

// form submitted

// set new values from form input

// write configuration back

// print message
echo "Configuration saved!";


>>> More PHP Articles          >>> More By icarus, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: