Hongmu Notes
Home Language Notes PHP modifies the configuration in the config file
Language Notes PHP PHP collection PHP and mysql

PHP modifies the configuration in the config file

PHP modifies the configuration in the config file

When developing a website using PHP, configuration files such as `config.php` typically require users to submit data through the backend for saving; in such cases, PHP is used to manipulate these files!

At this point, we need to use PHP to modify this PHP file.

For example, the code in my config.php file looks like this:

<?php
if(!defined('InEmpireCMS'))
{
	exit();
}
define('EmpireCMSConfig',TRUE);
$ecms_config=array();

//数据库设置
$ecms_config['db']['usedb']='mysql';	//数据库类型
$ecms_config['db']['dbver']='5.0';	//数据库版本
$ecms_config['db']['dbserver']='localhost';	//数据库登录地址
$ecms_config['db']['dbport']='';	//端口,不填为按默认
$ecms_config['db']['dbusername']='test_4s5_cn';	//数据库用户名
$ecms_config['db']['dbpassword']='wnxxtJTa3y4SPPrr';	//数据库密码
$ecms_config['db']['dbname']='test_4s5_cn';	//数据库名
$ecms_config['db']['setchar']='utf8';	//设置默认编码
$ecms_config['db']['dbchar']='utf8';	//数据库默认编码
$ecms_config['db']['dbtbpre']='phome_';	//数据表前缀
$dbtbpre=$ecms_config['db']['dbtbpre'];	//数据表前缀
$ecms_config['db']['showerror']=1;	//显示SQL错误提示(0为不显示,1为显示)

To make the change, I wrote a function!

function

 隐藏内容:登录后可查看
function modifyDatabaseConfig($filename, $newConfig) {
  // 读取配置文件内容
  $file = file_get_contents($filename);
  // 替换配置内容
  
  // 配置文件里的变量名前缀
    $prefix = '$ecms_config[\'db\']';
    
    // 遍历新配置,将对应的变量替换为新值
    foreach ($newConfig as $key => $value) {
        $pattern = '/' . preg_quote($prefix) . '\[\'' . preg_quote($key) . '\'\]\s*=\s*\'[^;]+;/';
        $replace = $prefix . '[\'' . $key . '\'] = \'' . $value . '\';';
        $file = preg_replace($pattern, $replace, $file);
    }
  // 保存文件
  file_put_contents($filename, $file);
}

Function parameter description:

  • $filename: The full path and filename of the configuration file;
  • $newConfigAn array containing the database configuration items to be modified and their corresponding new values. For example, to change the database address to 127.0.0.1Change password newpasswordThe function can be called as follows:modifyDatabaseConfig('config.php', ['dbserver' => '127.0.0.1', 'dbpassword' => 'newpassword']);

This function reads the contents of the configuration file, uses regular expressions to match all database configuration entries, replaces them with new values, and finally writes the modified content back to the configuration file.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232
PHP special character escaping and restoration

PHP special character escaping and restoration Language Notes PHP PHP collection PHP and mysql

Escape character is a special character constant. Escape characters are backslashed " & quot; At the beginning, followed by one or more characters. The escaped character has a specific meaning, which is different from the original meaning of the character, so it is called "escaped" character. The use of escape characters 1: turn ordinary characters into special purposes, such as back key and enter key. 2. Used to convert a character with special meaning back to its original meaning. 3. Before data is written into the database, escape characters (function …
👁 180
mysqli in PHP

mysqli in PHP Language Notes PHP PHP collection PHP and mysql

The `mysqli_num_rows()` function is exclusively used with `SELECT` query methods, whereas the `mysqli_affected_rows()` function returns the number of rows affected by the previous SQL statement across the entire database; this function is primarily used with `INSERT`, `UPDATE`, and `DELETE` operations.
👁 146

Recommended reading

(Adaptive mobile version) HTML5 green, professional machinery manufacturing industry PBootCMS template – Download compressed website source code for compressor equipment – 0200

(Adaptive mobile version) HTML5 green, professional machinery manufacturing industry PBootCMS template – Download compressed website source code for compressor equipment – 0200 Practical Collection pbootcms Template

An adaptive HTML5 green, professional mechanical manufacturing PbootCMS industry template designed specifically for compressor equipment manufacturers. Its modern and professional design effectively showcases compressor products, technical specifications, and industrial applications. This template helps mechanical equipment manufacturing enterprises demonstrate their capabilities online and attract targeted customers. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password:...
👁 40
Responsive Exhaust Gas Treatment Environmental Protection Equipment Website Template 0027

Responsive Exhaust Gas Treatment Environmental Protection Equipment Website Template 0027 Practical Collection Yiyou template

An eyouCMS responsive website template designed for environmental protection equipment manufacturers specializing in exhaust gas treatment systems. Its modern, eco-friendly design is ideal for showcasing exhaust gas treatment equipment, technical solutions, and project cases. This template helps environmental engineering firms present their solutions online and attract both industrial and municipal clients. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Common Installation Questions for YououCMS...
👁 28
Marketing-oriented precision machinery and equipment website template – 1070

Marketing-oriented precision machinery and equipment website template – 1070 Practical Collection Yiyou template

This EYO CMS template is ideal for the precision machinery and equipment marketing sector. Its professional, tech-oriented design effectively showcases precision equipment products, technical specifications, and industrial applications. It helps machinery manufacturing enterprises promote their brands online and attract industrial customers. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EYO CMS | EYO CMS (Eyo...
👁 43
(Adaptive Mobile Version) HTML5 Blue Wide-Screen Clean & Eco-Friendly Tech Energy pbootCMS Corporate Website Template – Responsive Blue Group Universal Website Source Code Download: 0201

(Adaptive Mobile Version) HTML5 Blue Wide-Screen Clean & Eco-Friendly Tech Energy pbootCMS Corporate Website Template – Responsive Blue Group Universal Website Source Code Download: 0201 Practical Collection pbootcms Template

This adaptive HTML5 corporate template for mobile devices – featuring a blue wide-screen design with eco-friendly technology and energy-themed themes, powered by PbootCMS – employs a responsive design approach. Its modern, tech-driven aesthetic makes it ideal for companies specializing in sustainable energy solutions or large corporations seeking to showcase their brand identity and business segments. It is an excellent choice for businesses looking to create an international, high-tech branded official website. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: ww...
👁 68
Responsive CNC mechanical power tool holder website template 0028

Responsive CNC mechanical power tool holder website template 0028 Practical Collection Yiyou template

This set of eyoucms responsive templates is suitable for CNC machinery and power tool holder companies. The design style is professional and sophisticated, and can display CNC products, power tool holders and mechanical accessories. It helps CNC machinery companies demonstrate their technical strength online and attract manufacturing customers. Template display Installation instructions Website backend: /login.php Account: admin Password: admin Related articles Yiyou CMS installation FAQ summary Yiyou CMS...
👁 41