Miscellaneous
  Home arrow Miscellaneous arrow Page 2 - Configuration File Processing with PHP
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 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? 
MISCELLANEOUS

Configuration File Processing with PHP
By: bluephoenix
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 6
    2003-12-13

    Table of Contents:
  • Configuration File Processing with PHP
  • A Simple Configuration File
  • A Grouped Configuration File
  • Conclusion

  • 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


    Configuration File Processing with PHP - A Simple Configuration File


    (Page 2 of 4 )

    Probably the simplest form of a configuration file is a list of options and their corresponding values. An example of this type is the configuration file for a Linux kernel before it is compiled.

    # ATA/IDE/MFM/RLL support
    #
    CONFIG_IDE=y

    #
    # IDE, ATA and ATAPI Block devices
    #
    CONFIG_BLK_DEV_IDE=y
    CONFIG_BLK_DEV_IDEDISK=y
    CONFIG_BLK_DEV_IDECD=n

    Each line that starts with a hash is considered a comment and is disregarded by the script. Other lines list options and set their desired values. For example, the CONFIG_IDE option has been set to "y" and the CONFIG_BLK_DEV_IDECD has been set to "n."

    PHP code designed to read such a configuration file might look like this:

    <?php
    $config_file 
    "my_config.conf";
    $comment "#";

    $fp fopen($config_file"r");

    while (!
    feof($fp)) {
      
    $line trim(fgets($fp));
      if (
    $line &amp;&amp; !ereg("^$comment"$line)) {
        
    $pieces explode("="$line);
        
    $option trim($pieces[0]);
        
    $value trim($pieces[1]);
        
    $config_values[$option] = $value;
      }
    }
    fclose($fp);

    if (
    $config_values['CONFIG_IDE'] == "y")
      echo 
    "CONFIG_IDE is set&lt;br /&gt;"";
    else
      echo "
    CONFIG_IDE is not set&lt;br /&gt;";
    ?>

    In our example code, $config_file holds the name of the configuration file and $comment holds the character used to designate comment lines.

    You'll notice how we trim the white space from our input several times. People like to format their configuration files with indents and other white spaces for the sake of readability and we would want to accommodate that.

    After we obtain a line from the configuration file, we check to see if the line contains any data and if it does not start with our comment character. If it passes both of these tests then we assume it is a configuration directive.

    Once we've established we have a configuration directive we break it up into it's two distinct pieces and store them in an array for future use. In the example I've used and array named $config_values, and the option itself acts as the key to the value's entry. The information from the configuration file can used throughout the script simply by referencing that array.

    More Miscellaneous Articles
    More By bluephoenix


       ·  } else { $pieces = explode(&quot;=&quot;, $line); $pieces[0]...
       · Even better is to useexplode("=", $line, 2);Where it is about the third...
       · This is a nice article - but why go to all of that effort when PHP has a built-in...
     

    MISCELLANEOUS ARTICLES

    - Install Slackware on Your Old PC
    - Firefox Plugins You`re Not Using (and Should...
    - Working with MP3 ID3 Tags in FTP Server Usin...
    - How Switching to Linux Can Make Your Computi...
    - Set Up Your Home Office on Linux: a Guide fo...
    - Putty File Transfer Commands in SSH Protocol
    - Setting Up Ubuntu for Your Home Office
    - Installing Mint Linux
    - Crucial Traits of Awk
    - Using PHP to Stream MP3 Files and Prevent Il...
    - 10 Must Have Firefox Improvements
    - All About OpenOffice 3.0
    - Shell Script Writing
    - Loops in the UNIX Shell
    - The Test in the UNIX Shell





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek