I have a config.php file which stores values in variables reguards database connections like user, dbname, etc. I have a form for an administrator to change these settings via the form in their browser. I was advised on ##php to take a look at var_export.

So i can pass the values of what the user entered into var_export from an array. for example:

$myarray = array($user, $host, $dbname, $pass);
$u = var_export($myarray, true);

My question is how do i write back the values of var_export to my config.php file? Would i use one of write functions in php perhaps, and how would i go about doing it?

    audiomove wrote:

    My question is how do i write back the values of var_export to my config.php file? Would i use one of write functions in php perhaps...

    yes, look at:
    [man]fopen[/man]
    [man]fwrite[/man]

      thx, i now am using fwrite to write it back to the config file like this

      <?php

      $host = $POST['txtHost'];
      $user = $
      POST['txtUser'];
      $pass = $POST['txtPassword'];
      $db = $
      POST['txtDb'];

      $myarray = array($host, $user, $pass, $db);
      $u = var_export($myarray, true);

      $filename = @readfile("../config.php");
      if(!filename) {
      print "Could not open configuration file.";
      } else {
      //print "file opened";

      $handle = fopen($filename, "wb");
      $numbytes = fwrite($handle, $u);
      fclose($handle);

      }
      ?>

      It runs and gives no errors but doesnt change anything in my config.php file. Have I left out something?

        3 things:

        1. you are calling [man]readfile[/man] before you write to the file so consquently you will still see the old data.

        2. why are you using "wb" as the [man]fopen[/man] argument? that would imply that you are writing to a binary file.

        3. be advised that [man]var_export[/man] will return only the array creation string, not the variable name within that code nor any opening closing PHP tags. you must add that to the data stream you are writing to your file if you wish it to be a stand alone valid PHP script.

          thx again for the reply. I can understand your points, I too was worried how it would be formatted back to the config file. How do i append the information to the array with the variable names and php tags?

            in this case $u is simply a string. you can add/concatenate just like any other string:

            $myarray = array(1,2,3,4,5);
            $u = var_export($myarray, true);
            $u = '<?php $myarray = ' . $u . ' ?>';
            

              wont that give me back something like

              <?php $myarray = array (
              0 => 'localhost',
              1 => 'value',

              etc. etc..
              ) ?>

              where i want

              <?php
              $host = 'localhost';
              $user = 'value';
              etc...
              ?>

                i suggest you re-organize how this data is coming from the form. consider this:

                <?php
                if (!isset($_POST['submit']))
                {
                	echo '
                	<form action="" method="POST">
                	Host: <input type="text" name="config[txtHost]"><br>
                	User: <input type="text" name="config[txtUser]"><br>
                	Password: <input type="text" name="config[txtPassword]"><br>
                	Db: <input type="text" name="config[txtDb]"><br>
                	<input type="submit" name="submit" value="submit">
                	</form>
                	';
                }
                else
                {
                	$string = '<?php' . "\n";
                	foreach ($_POST['config'] as $key => $value)
                	{
                		$string .= '$' . $key . " = '" . $value . "';" . "\n";
                	}
                	$string .= '?>';
                
                // write $string back to file
                }
                ?>
                

                if i were doing this i would just keep everything as an array (in the config file) rather than a bunch of individual global variables.

                  Write a Reply...