站長資訊網
最全最豐富的資訊網站

php如何修改hosts

php修改hosts的方法:首先刪除配置文件里域的hosts,將域配置為指定ip;然后添加一條host記錄;最后寫入host文件,代碼為【public function write()】。

php如何修改hosts

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

php修改hosts的方法:

有這樣一個需求,我有多個網址希望在不同的時候對應不同的 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 記錄數組     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視頻教程

贊(0)
分享到: 更多 (0)
網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
国产成人精品高清在线观看96| 日韩资源在线观看| 日韩无码系列综合区| .精品久久久麻豆国产精品| 久久久久无码精品国产| 久久精品国产成人AV| 亚洲级αV无码毛片久久精品| 久久97精品久久久久久久不卡| 亚洲第一区精品观看| 中文字幕一区日韩精品| 亚洲精品国产日韩无码AV永久免费网 | 在线观看亚洲精品国产| 国产精品亚洲精品日韩已方| 香蕉99久久国产综合精品宅男自 | 精品一区二区三区免费观看| 国产成人精品a视频一区| 一本大道无码日韩精品影视| 一区二区精品久久| 西瓜精品国产自在现线| 国产精品福利一区| 久久精品亚洲一区二区| 91麻豆精品国产自产在线观看一区| 国产A级毛片久久久精品毛片| 日产国产精品亚洲系列| 日韩aa在线观看| 精品女同一区二区三区在线| 日韩精品免费一线在线观看| 日韩中文无码有码免费视频| 在线观看国产日韩| 国产精品videossex白浆| 2021午夜国产精品福利| 久久国产亚洲精品无码| 91精品天美精东蜜桃传媒入口| 久久精品天天中文字幕人妻| 最新国产精品亚洲| 正在播放国产精品每日更新| 久久夜色撩人精品国产| 久久精品国产一区二区三区| 国产在线精品一区二区不卡麻豆 | 久久久WWW免费人成精品| 国产精品女人呻吟在线观看|