If I have a php file called variables.php and in it is the following <?php $myvariable = "variablecontent" ?> and I want to open that variable up in a form and edit it and maybe change it so that myvariable now has the value 'i_have_changed' how would I do it? I want the user to be able to do all of this from the browser.

Basically I want to open the variable and put the contents into a form and then edit it and write the change.

Thanks for any help.

-Adrian

    Erm, you want to rewrite .php files to replace variable contents?

    I would recommend using INI files. I made a post that (to my knowledge) accurately reads & modifies INI files, and you can view the original post here. I'll show you how to modify it to suit your needs, though.

    Everything from this line:

    $config['serveraddress'] = 'www.bradgrafelman.com'; // Example of changing/adding a setting 

    and on in that post is the modifying portion of the script; remove it or move it into a separate function to separate the read/write parts of the script. Here is what it would look like:

    function read_ini_file($path, &$config) {
    	if(!is_file($path)) die('Error: File does not exist (' . $path . ')');
    	if(filesize($path) === 0) return 0;
    	$file = file($path) or die('Error: Could not open file for reading (' . $path . ')'); 
    	$config = array();
    
    $file = implode('', $file);
    $file = explode("\n", $file);
    
    foreach($file as $line) {
    	if(strstr($line, ' =')) {
    		$split = explode(' =', $line);
    		$config[$split[0]] = trim($split[1]);
    	} // End if
    } // End foreach
    } // End function
    
    function write_ini_file($path, &$config) {
    	if(false === ($fp = fopen($path, 'w'))) die('Error: Could not open file for reading (' . $path . ')');
    
    foreach($config as $setting => $value) { 
    	if($value != NULL) fwrite($fp, "$setting = $value\n"); else fwrite($fp, "$setting =\n"); 
    } // End foreach
    fclose($fp); 
    } // End function 

    To read/change the values using a form, you would do something like this:

    $path = '/path/to/config.ini';
    if(isset($_POST['submit'])) {
        read_ini_file($path, $variables);
        $variables['field1'] = $_POST['field1'];
        write_ini_file($path, $variables);
        echo 'Variables successfully updated!';
    } else {
        read_ini_file($path, $variables);
        // Output HTML form, using a foreach() loop to get all varialbes, like so:
        foreach($variables as $name => $value) {
            echo $name . ': <input type="text" name="' . $name . '" value="' . $value . "\">\n";
        }
    }

    Confused yet? :p

    NOTE: This .ini function is NOT compatible with multi-line variable contents. If you want to use multiple lines, encode the line endings in the write function as special phrases, such as '~!br!~' so you can [man]str_replace/man the phrase to "\n" in the read function. Let me know if you need help with this.

    Also note that this is merely a suggestion. If you still wanted to use PHP files, you could probably use [man]preg_replace/man to match the text in the PHP file and update it's value. Seemed messy to me and very hard to keep secure, so I suggested my INI method of setting up variables.

      I wanted to have multiple variables in each file because I wanted a config.php file and wanted the user to be able to write their db information to the different variables. I will try the replace thing and if it doesnt work well/at all then I will try the ini method. Thanks for the help.

        Ok. The INI method still allows for multiple variables... including preserving spacing (as my original post said in the 'NOTE' section of the code), just setup the config.ini like so:

        username = My Username
        title = Hey there! This is a title.
        more fields = more data

          I've never used an ini file for config before. 2 things, how would the file look. A php file with variables looks like this: <?php $variable = "value" ?> how would the INI eqiuvalent look? Second, how do i convert ini variables to php ones? or if i did include("config.ini") into a php file would it work anyway?

          -Adrian

            I've given you all this information.

            1. It would look like the 'quote' section of my previous post.

            2. If you put the two functions in the script you're running, or in a 'functions.php' and you include('functions.php') on every script that needs to read/write to the INI file, you can use the code like I used in my previous post.

            If you chose the 'functions.php' method, it would look like so:

            include('/path/to/functions.php');
            
            read_ini_file('/path/to/config.ini', $variables);
            
            // All INI variables are now loaded into $variables as an array:
            
            echo 'username: ' . $variables['username'] . "<br>\n"
                   . 'title: ' . $variables['title'] . "<br>\n";

            and so on.

            To use these functions in conjunction with a form, use the code I posted in my previous post). I'm talking about the last PHP block, which starts out like so:

            $path = '/path/to/config.ini'; 
            if(isset($_POST['submit'])) { 

            and so on.

            As I said, all of this information I've already posted.

              Thanks. I'll try this tomorow, to tired at the moment!

                Write a Reply...