PHP implementation changes the Hosts file

Source: Internet
Author: User
Tags addgroup
This article mainly introduces the implementation of PHP to change the Hosts file method, combined with specific examples of the PHP operation hosts file related to read, set, delete and other implementation skills, the need for friends can refer to the next

The example in this article describes how PHP implements changing the Hosts file. Share to everyone for your reference, as follows:

There is such a demand, I have a number of Web sites want to correspond to different IP at different times, if each with the hosts, this work seems a bit cumbersome. Wrote the following script to bulk 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 (); The configuration file path, which defaults to __file__.    '. Ini ';    protected $configFile;    The configuration array read from the INI configuration file protected $config = Array ();    Config file needs to configure the domain name protected $domain = Array ();    Configuration file gets the IP data 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/public function Delallgroup () {foreach ($this->domain as $domain) of the domain in the configuration file {        $this->delrecord ($domain);         }}/** * Configure the domain to specify IP * @param type $ENV * @return \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;        }/** * Adds a host record * @param type $IP * @param type $domain */function AddRecord ($domain, $ip) {        $this->hosts[$domain] = $ip;    return $this; }/** * Deletes a host record * @param type $domain */function Delrecord ($domain) {unset ($this->hosts        [$domain]);    return $this;        }/** * Write to the 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 # Please ignore this = 1, because the use of parse_ini_file this function to parse, if the back without a value, will not get this record b.example.com=1c.example.com=1 # IP Record [ip]local=127.0.0.1dev=192.168.1.100

How to use:


PHP hosts.php Local # Domain name will point to native 127.0.0.1php hosts.php Dev # domain will point to dev 192.168.1.100php hosts.php # Delete domain's hosts configuration

After writing, found that this is clearly only need to find a replacement to complete the work

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.