Home  >  Article  >  Backend Development  >  PHP modify configuration file sample code

PHP modify configuration file sample code

WBOY
WBOYOriginal
2016-07-25 08:54:181419browse
  1. $fp = fopen("aaa.conf", 'r');

  2. $configfile = fread($fp, filesize("aaa.conf"));
  3. fclose($fp);
  4. //Do it through regular replacement
  5. $configfile = preg_replace("/\n[password](.+?)\n/is", "", $configfile);//This is only To match the content between [password] and the next blank line, just write /[password](.+?)\n/is, but I want to remove the blank line in front of this line, so add Got a n

  6. //Rewrite the file back to its original location

  7. $fp = fopen("aaa.conf", 'w');
  8. fwrite($fp, trim($configfile) );
  9. fclose($fp);
  10. //Add two new password lines at the end of the file
  11. $newpassword = "456";
  12. $filename="aaa.conf";//Define the operation file
  13. $fcontent = file( $filename); //file() reads the entire file into an array
  14. $fp = fopen("$filename","a");
  15. $str = "nn[password]n$newpassword";
  16. fwrite( $fp, $str);
  17. //by bbs.it-home.org

Copy code

Today I was changing the password of a php web shell program, and I encountered a problem. The password and The program is in the same file. How can it be modified seamlessly without affecting the normal execution of the program? The format of the configuration file 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, "rn");
Copy the code

and write $str back to the file

Indeed, according to my idea, the code should be like this, but when I execute it, it doesn’t work.

Why half? I have been thinking for a long time whether I can do it through regular expressions. So I also considered that the form $manage[''user''] does not appear many times in the program, and it may be modified through regular replacement.

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

Code:

  1. //Open file
  2. $fp = fopen($manage["file"], 'r');

  3. / / Read the file into $configfile

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

  6. // Replace with regular expression Do it

  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. // Rewrite the file back to its original location
  10. $fp = fopen($manage["file"], 'w');
  11. fwrite($fp , trim($configfile));
  12. fclose($fp);

Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn