站長(zhǎng)資訊網(wǎng)
最全最豐富的資訊網(wǎng)站

php如何修改hosts

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

php如何修改hosts

本教程操作環(huán)境:windows7系統(tǒng)、PHP5.6版、DELL G3電腦。

php修改hosts的方法:

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

<?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 配置文件讀取出來(lái)的配置數(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;     }     /**      * 寫(xiě)入 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 # 請(qǐng)無(wú)視這個(gè) =1,因?yàn)槭褂昧?parse_ini_file 這個(gè)函數(shù)來(lái)解析,如果后面不帶值,就獲取不到這條記錄了 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 # 域名將指向開(kāi)發(fā)機(jī) 192.168.1.100 php hosts.php # 刪除域名的 hosts 配置

相關(guān)視頻推薦:PHP視頻教程

贊(0)
分享到: 更多 (0)
網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)
日韩精品一二三四区| 日韩人妻高清精品专区| 国产亚洲精品资源在线26u| 成人国产精品999视频| www.日韩av.com| 欧洲精品免费一区二区三区| 国产精品久久亚洲不卡动漫| 久久综合国产乱子伦精品免费| 热久久这里只有精品| 在线观看国产精品日韩av| 精品视频在线观看你懂的一区| 日韩亚洲精品福利| 欧日韩在线不卡视频| 中文字幕日韩高清| 国产精品jizzjizz| 热99re久久精品2久久久| 国产精品无码AV不卡| 久热爱精品视频在线| 亚洲日韩精品无码专区加勒比| 国产精品影音先锋| 国产精品视频yuojizz| 国产成人精品天堂| 久久精品国产日本波多野结衣| 久久精品国产第一区二区三区| 99精品免费观看| 亚洲国产精品免费视频| 无码精品人妻一区二区三区人妻斩 | 91精品国产高清久久久久| 99在线观看精品视频| 99热这里只有精品7| 久久久久无码精品国产| 99精品在线观看视频| 91精品导航在线网址免费| 亚洲乱人伦精品图片| 992tv精品视频tv在线观看| 羞羞麻豆国产精品1区2区3区| 亚洲精品无码一区二区| 国产精品视频一区| 国产精品极品美女免费观看| 国产在线观看91精品不卡| 国产九九久久99精品影院|