php如何修改hosts

2021-03-09 19:00:23

php修改hosts的方法:首先刪除組態檔裡域的hosts,將域設定為指定ip;然後新增一條host記錄;最後寫入host檔案,程式碼為【public function write()】。

本教學操作環境:windows7系統、PHP5.6版、DELL G3電腦。

php修改hosts的方法:

有這樣一個需求,我有多個網址希望在不同的時候對應不同的 ip,如果一個個配 hosts,這工作顯得有些繁瑣。寫了如下指令碼來批次更改。

<?php
define('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 記錄陣列
    protected $hosts = array();
    // 組態檔路徑,預設為 __FILE__ . '.ini';
    protected $configFile;
    // 從 ini 組態檔讀取出來的設定陣列
    protected $config = array();
    // 組態檔裡面需要設定的域名
    protected $domain = array();
    // 組態檔獲取的 ip 資料
    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 這個函數來解析,如果後面不帶值,就獲取不到這條記錄了
b.example.com=1
c.example.com=1
# ip 記錄
[ip]
local=127.0.0.1
dev=192.168.1.100

使用方法:

php hosts.php local # 域名將指向本機 127.0.0.1
php hosts.php dev # 域名將指向開發機 192.168.1.100
php hosts.php # 刪除域名的 hosts 設定

相關視訊推薦:

以上就是php如何修改hosts的詳細內容,更多請關注TW511.COM其它相關文章!