博主信息
博文 33
粉丝 0
评论 0
访问量 32289
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
php实现host文件更改
非常缪
原创
1921人浏览过

host百度解释:

Hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联“数据库”,当用户在浏览器中输入一个需要登录的网址时,系统会首先自动从Hosts文件中寻找对应的IP地址,一旦找到,系统会立即打开对应网页,如果没有找到,则系统会再将网址提交DNS域名解析服务器进行IP地址的解析。

实现host文件的基本操作:

原理:

  1:读取host文件,parse_ini_file  解析为数组形式   保存到属性中
  2:对host文件的操作 就是对数组的操作  
  3:操作结束后,对数组进行处理 写入到文件中  file_put_contents

代码详解:

<?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();
   }

   /*初始化host文件*/
   public function initHosts()
   {
       //将host文件 处理成数组
       $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) {
           //    echo PHP_EOL; windows平台相当于    echo "rn";
           $str .= $ip . "t" . $domain . PHP_EOL;
       }
       file_put_contents($this->file, $str);
       return $this;
   }

}

                                                   


本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学