Example of how PHP implements changes to the hosts file,
This example describes how to change the hosts file in PHP. We will share this with you for your reference. The details are as follows:
There is such a requirement that I have multiple websites that want to correspond to different ip addresses at different times. If hosts is configured one by one, this work is a little complicated. Write the following script to batch change.
<? Phpdefine ('host _ file', 'c: \ Windows \ System32 \ drivers \ etc \ hosts'); $ hm = new HostManage (HOST_FILE ); $ env = $ argv [1]; if (empty ($ env) {$ hm-> delAllGroup ();} else {$ hm-> addGroup ($ env );} class HostManage {// hosts file path protected $ FILE; // hosts record array protected $ hosts = array (); // configuration file path. The default value is _ FILE __. '. ini '; protected $ configFile; // configuration array read from the ini configuration file protected $ config = array (); // The domain name prot to be configured in the configuration file Ected $ domain = array (); // ip address data obtained from the configuration file protected $ ip = array (); public function _ construct ($ file, $ config_file = null) {$ this-> file = $ file; if ($ config_file) {$ this-> configFile = $ config_file;} else {$ this-> configFile = _ FILE __. '. ini ';} $ this-> initHosts ()-> initCfg ();} public function _ destruct () {$ this-> write ();} public function initHosts () {$ lines = file ($ this-> file); foreach ($ Lines as $ line) {$ line = trim ($ line); if (empty ($ line) | $ line [0] = '#') {continue ;} $ item = preg_split ('/\ s +/', $ line); $ this-> hosts [$ item [1] = $ item [0];} return $ this;} public function initCfg () {if (! File_exists ($ this-> configFile) {$ this-> config = array ();} else {$ this-> config = (parse_ini_file ($ this-> configFile, true);} $ this-> domain = array_keys ($ this-> config ['domain ']); $ this-> ip = $ this-> config ['IP']; return $ this;}/*** Delete the hosts of the domain in the configuration file */public function delAllGroup () {foreach ($ this-> domain as $ domain) {$ this-> delRecord ($ domain );}} /*** configure the domain as the specified ip Address * @ param type $ env * @ retur N \ HostManage */public function addGroup ($ env) {if (! Isset ($ this-> ip [$ env]) {return $ this;} foreach ($ this-> domain as $ domain) {$ this-> addRecord ($ domain, $ this-> ip [$ env]);} return $ this ;} /*** Add a host record * @ param type $ ip * @ param type $ domain */function addRecord ($ domain, $ ip) {$ this-> hosts [$ domain] = $ ip; return $ this ;} /*** delete a host record * @ param type $ domain */function delRecord ($ domain) {unset ($ this-> hosts [$ domain]); return $ this;}/*** write host file */public function write () {$ str = ''; foreach ($ this-> hosts as $ domain => $ ip) {$ str. = $ ip. "\ t ". $ domain. PHP_EOL;} file_put_contents ($ this-> file, $ str); return $ this ;}}
The sample configuration file is as follows:
# Domain name [domain] a.example.com = 1 # ignore this = 1 because the parse_ini_file function is used for parsing. If the value is not followed, this record cannot be obtained. B .example.com = 1c.example.com = 1 # ip record [ip] local = 127.0.0.1dev = 192.168.1.100
Usage:
Php hosts. php local # The domain name will point to 127.0.0.1php hosts. php dev # The domain name will point to the development machine 192.168.1.100php hosts. php # Delete the hosts configuration of the domain name
After writing it, I found that this is clearly the work that can be done by simply finding and replacing it all at once.