Results 1 to 5 of 5

Thread: Edit PHP File

  1. #1

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Edit PHP File

    In my Admin area I display all the configuration values for my login script (eg. Site URL, Site Name etc...) After the Admin changes any of the values how do I then save the changes back to my configuration file overwriting the values that were already there?

    Thanks.

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    I am creating a class at the moment which writes and saves configuration files similar to the httpd.conf file. I'll post it here when I'm done. You may also want to look at the PHP parse_ini_file() function. There are some good examples in the user comments of functions which also write ini files too.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3
    Addicted Member señorbadger's Avatar
    Join Date
    Oct 2003
    Location
    Mud pools of wellingborough
    Posts
    193
    This is the way i do it

    create a file with the settings laied out in this way


    setting=value
    ;comment

    then in php have a function to read the list

    PHP Code:
    <?php 
    $cfgfile 
    "scripts/config.txt";
    //open the config file to read
    $cfg fopen($cfgfile,"r");

    if (
    $cfg)
    {
    $data fread($cfg,filesize($cfgfile));
    //split by new lines
    $lines split("\n",$data);
    //$lines is an array of settings and values
    for ($d=0;$d<sizeof($lines);$d++)
    {
    if (
    substr($lines[$d],0,1) != ";")
    {
    $bits explode("=",$lines[$d]);
    $CFG[$bits[0]] = $bits[1];
    // if its not a comment line
    }
    }
    }
    else
    {
    die(
    "Cannot Read Config File ( $cfgfile )");
    }
    fclose($cfg)
    // to use the settings its CFG[SETTING] the value will be stored in this 2D array
    ?>
    try that :P

    example would be

    CFG['setting'] would equal "value"

    EDIT : Removed extra )
    Last edited by señorbadger; Sep 16th, 2004 at 03:47 AM.

  4. #4
    Addicted Member señorbadger's Avatar
    Join Date
    Oct 2003
    Location
    Mud pools of wellingborough
    Posts
    193
    made a class (1st one so dont laugh please)

    here is how to use

    PM me for help if u need k man

    PHP Code:
    <?php 
    require_once 'config.class.php';
    $config = new config;
    $CFG $config->read_config("config.cfg");
    ?>
    $CFG = array of $CFG[Setting] settings names
    functions in class
    int add_setting($setting, $value, $file)
    $setting = setting name
    $value = setting value
    $file = file to save to
    This function will update if it already exsists

    int delete_setting($setting, $file)
    $setting = setting name
    $file = file to delete from

    int update_setting($setting, $newvalue, $file)
    $setting = setting name
    $newvalue = the new setting value
    $file = file to save to
    This function will create setting if not found

    int is_setting($setting, $file)

    array read_config($file)
    $file = file to read from
    will return array structure of object[setting] = value
    Last edited by señorbadger; Sep 16th, 2004 at 05:32 AM.

  5. #5
    Addicted Member señorbadger's Avatar
    Join Date
    Oct 2003
    Location
    Mud pools of wellingborough
    Posts
    193
    soz heres the attachment
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width