Php configuration file modification Sample code

Source: Internet
Author: User
Php configuration file modification Sample code

  1. $ Fp = fopen ("aaa. conf", 'r ');

  2. $ Configfile = fread ($ fp, filesize ("aaa. conf "));
  3. Fclose ($ fp );
  4. // Use regular expression replacement
  5. $ Configfile = preg_replace ("/\ n \ [password \] (. + ?) \ N/is "," ", $ configfile); // you only need to match the content between the [password] and the next empty line. you only need to write it as/\ [password \] (. + ?) \ N/is, but I want to remove the blank lines in front of this line, so I add \ n

  6. // Re-write the file back to its original location

  7. $ Fp = fopen ("aaa. conf", 'w ');
  8. Fwrite ($ fp, trim ($ configfile ));
  9. Fclose ($ fp );
  10. // Add the new password line at the end of the file
  11. $ Newpassword = "456 ";
  12. $ Filename = "aaa. conf"; // defines the operation file
  13. $ Fcontent = file ($ filename); // file () reads the entire file into an array
  14. $ Fp = fopen ("$ filename", "");
  15. $ Str = "\ n [password] \ n $ newpassword ";
  16. Fwrite ($ fp, $ str );
  17. // By bbs.it-home.org

Today, I encountered a problem when I made a password change for a php web shell program. The password and program are in the same file. how can I make seamless changes without affecting the normal execution of the program. The configuration file format is similar to the following:

  1. $ Lines = file ("config. php ");
  2. $ Count = sizeof ($ lines );
  3. For ($ I = 0; $ I <$ count; $ I ++ ){
  4. $ Tmp = explode ($ lines [$ I], '= ');
  5. If ($ tmp = null | sizeof ($ tmp )! = 2)
  6. Continue;
  7. If (trim ($ tmp [0]) = '$ manage ["user"]') {
  8. $ Lines [$ I] = $ tmp [0]. "=". $ manage ["user"];
  9. Break;
  10. }
  11. }
  12. $ Str = implode ($ lines, "\ r \ n ");

Then write $ str back to the file

Indeed, according to my ideas, the code should be like this, but it is not easy for me to execute it.

Why? After thinking about it for a long time, you can use a regular expression. Therefore, considering that the form $ manage [''user''] does not appear frequently in the program, it may be able to be modified by regular expression replacement.

Idea: Read all program code into a variable, and then replace the corresponding content in the string with regular expressions.

Code:

  1. // Open the file

  2. $ Fp = fopen ($ manage ["file"], 'r ');

  3. // Read the file into $ configfile

  4. $ Configfile = fread ($ fp, filesize ($ manage ["file"]);
  5. Fclose ($ fp );

  6. // Use regular expression replacement

  7. $ Configfile = preg_replace ("/[$] manage \ [\" user \ "\] \ s * \ = \ s * [\" ']. *? [\ "']/Is", "\ $ manage [\" user \ "] = \" $ user_name \ "", $ configfile );
  8. $ Configfile = preg_replace ("/[$] manage \ [\" pass \ "\] \ s * \ = \ s * [\" ']. *? [\ "']/Is", "\ $ manage [\" pass \ "] = \" $ user_pass \ "", $ configfile );
  9. // Re-write the file back to its original location
  10. $ Fp = fopen ($ manage ["file"], 'w ');
  11. Fwrite ($ fp, trim ($ configfile ));
  12. Fclose ($ fp );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.