Connecting Tech Pros Worldwide Forums | Help | Site Map

Modify PHP file from another PHP file?

Familiar Sight
 
Join Date: Sep 2008
Posts: 255
#1: 4 Weeks Ago
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_contents() or is there an easier way?

config.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. #####################
  3. #####################
  4.  
  5. //WARNING: DON'T CHANGE BELOW HERE
  6. //Database details
  7. $host = '';
  8. $username = '';
  9. $pass = ''
  10. $prefix = '';
  11. ?>

Familiar Sight
 
Join Date: Sep 2008
Posts: 255
#2: 4 Weeks Ago

re: Modify PHP file from another PHP file?


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:
Expand|Select|Wrap|Line Numbers
  1. "host = '%1'", "host = 'new value'"
Expand|Select|Wrap|Line Numbers
  1. $fname = "config.php";
  2. $fhandle = fopen($fname,"r");
  3. $content = fread($fhandle,filesize($fname));
  4.  
  5. $content = str_replace("host = ''", "host = 'test1'", $content);
  6. $content = str_replace("username = ''", "username = 'test2'", $content);
  7. $content = str_replace("pass = ''", "pass = 'test3'", $content);
  8. $content = str_replace("prefix = ''", "prefix = 'test4'", $content);
  9.  
  10. $fhandle = fopen($fname,"w");
  11. fwrite($fhandle,$content);
  12. fclose($fhandle);
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,083
#3: 4 Weeks Ago

re: Modify PHP file from another PHP file?


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.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,678
#4: 4 Weeks Ago

re: Modify PHP file from another PHP file?


access one PHP file from another:
Expand|Select|Wrap|Line Numbers
  1. // one.php
  2. <?php
  3.   include "two.php";
  4.  
  5.   // some code in two.php
  6. ?>
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.
Familiar Sight
 
Join Date: Sep 2008
Posts: 255
#5: 4 Weeks Ago

re: Modify PHP file from another PHP file?


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.
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,083
#6: 4 Weeks Ago

re: Modify PHP file from another PHP file?


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.
Reply