$ Fp = fopen ("aaa. conf", 'r ');
- $ Configfile = fread ($ fp, filesize ("aaa. conf "));
- Fclose ($ fp );
- // Use regular expression replacement
- $ 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
// Re-write the file back to its original location
- $ Fp = fopen ("aaa. conf", 'w ');
- Fwrite ($ fp, trim ($ configfile ));
- Fclose ($ fp );
- // Add the new password line at the end of the file
- $ Newpassword = "456 ";
- $ Filename = "aaa. conf"; // defines the operation file
- $ Fcontent = file ($ filename); // file () reads the entire file into an array
- $ Fp = fopen ("$ filename", "");
- $ Str = "\ n [password] \ n $ newpassword ";
- Fwrite ($ fp, $ str );
- // 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:
- $ Lines = file ("config. php ");
- $ Count = sizeof ($ lines );
- For ($ I = 0; $ I <$ count; $ I ++ ){
- $ Tmp = explode ($ lines [$ I], '= ');
- If ($ tmp = null | sizeof ($ tmp )! = 2)
- Continue;
- If (trim ($ tmp [0]) = '$ manage ["user"]') {
- $ Lines [$ I] = $ tmp [0]. "=". $ manage ["user"];
- Break;
- }
- }
- $ 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:
// Open the file
- $ Fp = fopen ($ manage ["file"], 'r ');
// Read the file into $ configfile
- $ Configfile = fread ($ fp, filesize ($ manage ["file"]);
- Fclose ($ fp );
// Use regular expression replacement
- $ Configfile = preg_replace ("/[$] manage \ [\" user \ "\] \ s * \ = \ s * [\" ']. *? [\ "']/Is", "\ $ manage [\" user \ "] = \" $ user_name \ "", $ configfile );
- $ Configfile = preg_replace ("/[$] manage \ [\" pass \ "\] \ s * \ = \ s * [\" ']. *? [\ "']/Is", "\ $ manage [\" pass \ "] = \" $ user_pass \ "", $ configfile );
-
- // Re-write the file back to its original location
- $ Fp = fopen ($ manage ["file"], 'w ');
- Fwrite ($ fp, trim ($ configfile ));
- Fclose ($ fp );
|