473,748 Members | 7,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Modify PHP file from another PHP file?

384 Contributor
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
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 5485
ziycon
384 Contributor
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 Recognized Expert Top Contributor
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 Recognized Expert Moderator Expert
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 Contributor
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 Recognized Expert Top Contributor
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
1675
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 that word i.e. Change it to some other shorter word. I dont think that I can declare a buffer, because it may happen that these files may be too big. I hope someone has some ideas how this might be done. Regards,
1
1627
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 you and another user are trying to modify the same data (translated from dutch). I can't do anythinh with this file. Even importing from another database or Compact and Repairing gives me the same message. Has anyone got a clue how to save the...
3
13392
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
2074
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
6203
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
2047
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. What is the problem and how may I do to modify the file without deteriorating its format? Thank you!
2
1633
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. I have a local HTML file, where I have some kind of link. When this
9
2026
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 HTMLID.Style = "10px"; however I need to apply it to a CSS class not a single ID. What is the code for that?
3
13660
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 B's app.config. I've referenced System.configuration.dll within Project B, and from one of the classes inside Project B's namespace, I'm trying to use ConfigurationManager to write to its app.config. Unfortunately, it only finds Project A's...
0
8989
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9537
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6795
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4599
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.