国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP實現(xiàn)更改hosts文件的方法示例

2024-05-04 23:59:18
字體:
來源:轉載
供稿:網(wǎng)友

本文實例講述了PHP實現(xiàn)更改hosts文件的方法。分享給大家供大家參考,具體如下:

有這樣一個需求,我有多個網(wǎng)址希望在不同的時候對應不同的 ip,如果一個個配 hosts,這工作顯得有些繁瑣。寫了如下腳本來批量更改。

<?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 文件路徑    protected $file;    // hosts 記錄數(shù)組    protected $hosts = array();    // 配置文件路徑,默認為 __FILE__ . '.ini';    protected $configFile;    // 從 ini 配置文件讀取出來的配置數(shù)組    protected $config = array();    // 配置文件里面需要配置的域名    protected $domain = array();    // 配置文件獲取的 ip 數(shù)據(jù)    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;    }    /**     * 刪除配置文件里域的 hosts     */    public function delAllGroup() {        foreach ($this->domain as $domain) {            $this->delRecord($domain);        }    }    /**     * 將域配置為指定 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;    }    /**     * 添加一條 host 記錄     * @param type $ip     * @param type $domain     */    function addRecord($domain, $ip) {        $this->hosts[$domain] = $ip;        return $this;    }    /**     * 刪除一條 host 記錄     * @param type $domain     */    function delRecord($domain) {        unset($this->hosts[$domain]);        return $this;    }    /**     * 寫入 host 文件     */    public function write() {        $str = '';        foreach ($this->hosts as $domain => $ip) {            $str .= $ip . "/t" . $domain . PHP_EOL;        }        file_put_contents($this->file, $str);        return $this;    }}

示例配置文件如下:

# 域名[domain]a.example.com=1 # 請無視這個 =1,因為使用了 parse_ini_file 這個函數(shù)來解析,如果后面不帶值,就獲取不到這條記錄了b.example.com=1c.example.com=1# ip 記錄[ip]local=127.0.0.1dev=192.168.1.100

使用方法:

php hosts.php local # 域名將指向本機 127.0.0.1php hosts.php dev # 域名將指向開發(fā)機 192.168.1.100php hosts.php # 刪除域名的 hosts 配置

寫完后,發(fā)現(xiàn),這明明就是只需要一次查找替換就能完成的工作嘛

希望本文所述對大家PHP程序設計有所幫助。


注:相關教程知識閱讀請移步到PHP教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 新宁县| 交口县| 宁陕县| 楚雄市| 金秀| 大英县| 陆川县| 高要市| 金昌市| 嘉善县| 鹤壁市| 钟山县| 阿鲁科尔沁旗| 化德县| 武陟县| 平邑县| 济宁市| 大渡口区| 泰州市| 东平县| 泸定县| 遂昌县| 南康市| 浮山县| 菏泽市| 云阳县| 前郭尔| 东源县| 精河县| 谢通门县| 信阳市| 社旗县| 宁晋县| 腾冲县| 怀集县| 巩留县| 巩留县| 尼木县| 黔西| 崇义县| 栾城县|