PHP
  Home arrow PHP arrow Page 7 - Configuration Manipulation With PHP Co...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
OLM
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Configuration Manipulation With PHP Config
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 58
    2003-09-04

    Table of Contents:
  • Configuration Manipulation With PHP Config
  • Plug And Play
  • Your Friendly Neighbourhood Hulk
  • Different Strokes
  • Array Of Hope
  • Up A Tree
  • Changing Things Around
  • Giving Birth
  • Link Zone

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    Configuration Manipulation With PHP Config - Changing Things Around
    (Page 7 of 9 )

    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.


    <?php

    // include class
    include("Config.php");

    // 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
    $c->writeConfig();

    ?>

    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.


    <html>
    <head>
    </head>

    <body>

    <?php

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

    // include class
    include("Config.php");

    // 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
    ?>
    <h2>Configuration</h2>
    <table border="0" cellspacing="5" cellpadding="5">
    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">

    <tr>
    <td>ISP user name</td>
    <td>
    <input type="text" name="user"
    value="<?=$ppp_userDirective->getContent();?>">
    </td>
    </tr>

    <tr>
    <td>ISP user password</td>
    <td>
    <input type="text" name="pass"
    value="<?=$ppp_passDirective->getContent();?>">
    </td>
    </tr>

    <tr>
    <td>ISP dial-up number</td>
    <td>
    <input type="text" name="number"
    value="<?=$numberDirective->getContent();?>">
    </td>
    </tr>

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

    <tr>
    <td>Connection device</td>
    <td>
    <input type="text" name="device"
    value="<?=$deviceDirective->getContent();?>">&
    lt;/td>
    </tr>

    <tr>
    <td>Connection script</td>
    <td>
    <input type="text" name="script"
    value="<?=$connect_scriptDirective->getContent();?>">
    </td>
    </tr>

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

    </form>
    </table>
    <?
    }
    else
    {
    // form submitted

    // set new values from form input
    $numberDirective->setContent($_POST['number']);
    $retriesDirective->setContent($_POST['retries']);
    $deviceDirective->setContent($_POST['device']);
    $connect_scriptDirective->setContent($_POST['script']);
    $ppp_userDirective->setContent($_POST['user']);
    $ppp_passDirective->setContent($_POST['pass']);

    // write configuration back
    $c->writeConfig();

    // print message
    echo "Configuration saved!";
    }
    }
    ?>

    </body>
    </html>

    More PHP Articles
    More By icarus, (c) Melonfire


     

       

    PHP ARTICLES

    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery
    - Using Timers to Benchmark PHP Applications
    - Benchmarking Applications with PHP
    - Setting Up a Web-Based File Manager: PHPfile...
    - Developing a Modular Class For a PHP File Up...
    - Setting Up a Web-Based File Manager: bfExplo...
    - Defining a Custom Function for File Uploader...
    - Parsing Child Nodes with the DOM XML extensi...
    - Creating an Error Handling Module for a PHP ...
    - Accessing Attributes and Cloning Nodes with ...
    - Retrieving Information on Selected Files wit...
    - Handling HTML Strings and Files with the DOM...
    - Building File Uploaders with PHP 5
    - Working with Multiple Document Nodes with th...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway