Modify PHP file from another PHP file?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ziycon
    Contributor
    • Sep 2008
    • 384

    Modify PHP file from another PHP file?

    I have two PHP files, modify.php & config.php, I have the below code in config.php and I'm wondering how I would go about searching config.php from modify.php and changing the values in config.php?

    Should I be using something like file_get_conten ts() or is there an easier way?

    config.php
    Code:
    <?php
    #####################
    #####################
    
    //WARNING: DON'T CHANGE BELOW HERE
    //Database details
    $host = '';
    $username = '';
    $pass = ''
    $prefix = '';
    ?>
  • ziycon
    Contributor
    • Sep 2008
    • 384

    #2
    This code seems to do the job, is this the best way to do it? How can I change the the values using preg_replace like:
    Code:
    "host = '%1'", "host = 'new value'"
    Code:
    $fname = "config.php";
    $fhandle = fopen($fname,"r");
    $content = fread($fhandle,filesize($fname));
    
    $content = str_replace("host = ''", "host = 'test1'", $content);
    $content = str_replace("username = ''", "username = 'test2'", $content);
    $content = str_replace("pass = ''", "pass = 'test3'", $content);
    $content = str_replace("prefix = ''", "prefix = 'test4'", $content);
    
    $fhandle = fopen($fname,"w");
    fwrite($fhandle,$content);
    fclose($fhandle);

    Comment

    • code green
      Recognized Expert Top Contributor
      • Mar 2007
      • 1726

      #3
      If it is a config file then it may be better in a text file rather than php.
      Then read the file, modify the contents then overwrite the file.
      Being in a text file also makes it easier for modifying by hand
      and is more in keeping with PHP itself whose config files are text.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        access one PHP file from another:
        Code:
        // one.php
        <?php
          include "two.php";
        
          // some code in two.php
        ?>
        personally I don't recommend putting sensitive information in a plain text file because this file can be easily read with a browser. PHP files have the advantage of not showing anything until you define output. although I don't recommend using variables to store something like passwords etc. constants are better suited for such a job.

        you could go one step further and store sensitive information in a database, but it will make accessing that data more complex.

        Comment

        • ziycon
          Contributor
          • Sep 2008
          • 384

          #5
          I'm using a PHP file for the config file so the data can't be read via the browser and also the details have to stored here as they are the database details that a user would enter upon installation of the app. which is why I can't store them in the database.

          Comment

          • code green
            Recognized Expert Top Contributor
            • Mar 2007
            • 1726

            #6
            As an extra precaution it is recommended to put such files outside the hierarchy of the public domain.
            In a folder above the www root for example.

            Comment

            Working...