473,382 Members | 1,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Modify PHP file from another PHP file?

384 256MB
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. ?>
Oct 29 '09 #1
5 5454
ziycon
384 256MB
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);
Oct 29 '09 #2
code green
1,726 Expert 1GB
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.
Oct 29 '09 #3
Dormilich
8,658 Expert Mod 8TB
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.
Oct 29 '09 #4
ziycon
384 256MB
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.
Oct 30 '09 #5
code green
1,726 Expert 1GB
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.
Oct 30 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: carmaski | last post by:
Hi, I have worked with C++ off and on, but I am not sure how this must be done. I need to modify some of the text in certain files. Specifically I need to search for a certain word, and modify...
1
by: Remco Groot Beumer | last post by:
Hello, I've received a databasefile from a friend, which seems to be corrupted. When i try to open the file it gives the error: The Microsoft Jet Database Engine has stopped the proces, since...
3
by: Maileen | last post by:
Hi, I've asked yesterday if someone already modify data into XML file using VB.NET. In fact, my XML file is like that. .... <DB> <DB_Loc>
3
by: muttu2244 | last post by:
hi everybody am trying to modify a particular string from a csv file. for ex say i have compName, ipAddr, MacAddr, Os sdfsdf , 129.122.12.34 , dsfiasdf, wsfdjhs
4
by: muttu2244 | last post by:
Hi everybody Am trying to read a csv file "temp.csv", which has the below info, compName macAddr ipAddr opSys
1
by: LB | last post by:
Hi, I want to modify my file ".vbproj". I use XML dlls of Visual Studio and that causes to modify the format of the file .vbproj and that causes problems at the compile time on another servor....
2
by: Martin Mosbęk Christiansen | last post by:
Hi I have been searching this group but I haven't found anything I can use... Is it possible to remote modify an already open window form from a local HTML file? Example (simplified): 1....
9
by: Will | last post by:
I have given up looking for a solution so I figured I would break down and ask. I am trying to modify a CSS class in page_load. I have no problem doing it with an ID with the simple ...
3
by: Jeff | last post by:
I have a solution with two projects. Project A is the startup project, while Project B serves as the project with the data logic. At run time, the first thing I need to do is write to Project...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.