成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導(dǎo)航首頁 ? 技術(shù)教程 ? PHP實(shí)現(xiàn)更改hosts文件的方法示例
全站頭部文字 我要出現(xiàn)在這里
PHP實(shí)現(xiàn)更改hosts文件的方法示例 771 2023-12-08   

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

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

<?php
define('HOST_FILE', 'C:WindowsSystem32driversetchosts');
$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();
    // 配置文件路徑,默認(rèn)為 __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,因?yàn)槭褂昧?parse_ini_file 這個函數(shù)來解析,如果后面不帶值,就獲取不到這條記錄了
b.example.com=1
c.example.com=1
# ip 記錄
[ip]
local=127.0.0.1
dev=192.168.1.100

使用方法:

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

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

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php文件操作總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》

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


主站蜘蛛池模板: 爱,藏起来 电影| 国产老头视频| 狂魔电影| 电影哪吒闹海二| 中国最贵的10大香烟| 熊出没之雪岭熊风 电影| ?1,| 麻豆视频观看| 诺曼瑞杜斯| 九州电影网| 魔幻手机3什么时候上映| 孙泽源个人简介| 同志激情电影| 《摧花狂魔》电影| 精卫填海演员表| 2003年黄金价格多少一克| 潇洒走一回广场舞完整版| 啊信| https://www.douyin.com| 芦苇编剧| 游泳池play高h| 色在线视频播放| 但愿人长久| 乱世危情电视剧演员表| 喜迎20大文艺汇演主持词| 地理填充图册| 正义回廊 电影| 孙涛个人简历| 惊悚电影| 四年级上册语文书有鬼| 血色樱花演员表| 性的张力短片集| 管路通| 成品直播大全免费观看| 一人比划一人猜100个| 女公安毛片免费观看| 谭天| 《起风了》数字简谱| 2025小升初真题卷英语| 《魔》| 中医基础理论试题题库及答案 |

?。?!站長長期在線接?。?!

網(wǎng)站、小程序:定制開發(fā)/二次開發(fā)/仿制開發(fā)等

各種疑難雜癥解決/定制接口/定制采集等

站長微信:lxwl520520

站長QQ:1737366103