在网站的后台中经常会遇到修改网站的配置信息,因为每次都要进行手工的修改,感觉很麻烦,所以就用php在网页中直接修改,,方便多了。。首先新建一个配置文件config.php内容如下
3 | define( "HOST" , "localhost" ); //主机 |
4 | define( "USER" , "wangjian" ); //用户名 |
5 | define( "PASS" , "wangjian" ); //密码 |
6 | define( "DB" , "123" ); //数据库 |
index.php文件如下
03 | $info = ( file_get_contents ( "config.php" )); //这个地方需要把<?php 给去掉否则是无法进行得到值的 |
04 | preg_match_all( "#define\(\"(.*?)\",\"(.*?)\"\)#" , $info , $a ); //其中?是为了避免贪婪匹配,只匹配下一个“号为止 #是分节符 |
15 | if (isset( $_POST [ 'sub' ])){ |
17 | $info = file_get_contents ( "config.php" ); |
18 | foreach ( $_POST as $k => $v ){ |
19 | $info = preg_replace( "#define\(\"{$k}\",\".*?\"\)#" , "define(\"{$k}\",\"{$v}\")" , $info ); |
21 | //在实际的项目中还要判断文件是否可读和可写 |
22 | file_put_contents ( "config.php" , $info ); |
23 | header( "Location:./index.php" ); |
27 | <form method= "post" name= "" action= "" > |
30 | foreach ( $a [0] as $k => $v ){ |
34 | <?php echo $array [ $a [1][ $k ]];?>:<input type= "text" name= "<?php echo $a[1][$k];?>" value= "<?php echo $a[2][$k];?>" ><br /> |
37 | <input type= "submit" value= "修改" name= "sub" > |