PHP實(shí)現(xiàn)更改hosts文件的方法示例
本文實(shí)例講述了PHP實(shí)現(xiàn)更改hosts文件的方法。分享給大家供大家參考,具體如下:
有這樣一個(gè)需求,我有多個(gè)網(wǎng)址希望在不同的時(shí)候?qū)?yīng)不同的 ip,如果一個(gè)個(gè)配 hosts,這工作顯得有些繁瑣。寫了如下腳本來(lái)批量更改。
<?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 記錄數(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;
}
/**
* 寫入 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 配置
寫完后,發(fā)現(xiàn),這明明就是只需要一次查找替換就能完成的工作嘛
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php文件操作總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP實(shí)現(xiàn)一個(gè)限制實(shí)例化次數(shù)的類示例
這篇文章主要介紹了PHP實(shí)現(xiàn)一個(gè)限制實(shí)例化次數(shù)的類,涉及php面向?qū)ο蟪绦蛟O(shè)計(jì)中靜態(tài)對(duì)象與靜態(tài)方法的相關(guān)使用技巧,需要的朋友可以參考下2019-09-09
關(guān)于URL最大長(zhǎng)度限制的相關(guān)資料查證
這篇文章主要介紹了關(guān)于URL最大長(zhǎng)度限制的相關(guān)資料查證,這里記錄一下,方便以后使用。2014-12-12
php cache類代碼(php數(shù)據(jù)緩存類)
php的執(zhí)行效率很高,速度很快,但是連接數(shù)據(jù)庫(kù)、查詢數(shù)據(jù)庫(kù)等還是比較耗時(shí)的。2010-04-04
PHP實(shí)現(xiàn)導(dǎo)出帶樣式的Excel
有時(shí)客戶會(huì)向你抱怨,軟件為他們導(dǎo)出的Excel格式太難看了,或許這篇文章會(huì)對(duì)你有所幫助。在用PHP導(dǎo)出數(shù)據(jù)的同時(shí)還可以設(shè)置顏色、字號(hào)大小、加粗、合并單元格等等。2016-08-08
使用PHP函數(shù)進(jìn)行網(wǎng)站性能監(jiān)控和優(yōu)化的代碼示例
在 PHP 中,有許多內(nèi)置函數(shù)和工具可以幫助我們實(shí)現(xiàn)網(wǎng)站的性能監(jiān)控和優(yōu)化,本文將介紹幾種常用的 PHP 函數(shù),并提供相應(yīng)的代碼示例,來(lái)幫助您更好地進(jìn)行網(wǎng)站性能監(jiān)控和優(yōu)化,需要的朋友可以參考下2024-06-06
Apache連接PHP后無(wú)法啟動(dòng)問(wèn)題解決思路
這篇文章主要介紹了Apache連接PHP后無(wú)法啟動(dòng)問(wèn)題解決思路及解決方案,簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。2015-06-06
PHP使用PDO訪問(wèn)oracle數(shù)據(jù)庫(kù)的步驟詳解
POD擴(kuò)展是在PHP5中加入,該擴(kuò)展提供PHP內(nèi)置類 PDO來(lái)對(duì)數(shù)據(jù)庫(kù)進(jìn)行訪問(wèn),不同數(shù)據(jù)庫(kù)使用相同的方法名,解決數(shù)據(jù)庫(kù)連接不統(tǒng)一的問(wèn)題。下面這篇文章主要給大家介紹了關(guān)于PHP使用PDO訪問(wèn)oracle數(shù)據(jù)庫(kù)的步驟,需要的朋友可以參考下。2017-09-09

