• 获取一个域名的主域名


    class DomainService
    {
        protected $cacheKey = 'domain-cache';
    
        protected $domainList;
    
        public function getDomainSuffix()
        {
            if (!$this->domainList) {
                $cacheList = Yii::$app->cacheFile->get($this->cacheKey);
                if ($cacheList) {
                    $this->domainList = $cacheList;
                } else {
                    $this->domainList = $this->syncRemoteDomain();
                    Yii::$app->cacheFile->set($this->cacheKey, $this->domainList);
                }
            }
    
            return $this->domainList;
        }
    
        public function syncRemoteDomain()
        {
            $res = CurlService::getIns()->get('https://publicsuffix.org/list/effective_tld_names.dat');
            if ($res->getErrorMsg()) {
                return [];
            }
    
            $body = $res->getBody();
            $domainSuffixList = explode("
    ", $body);
            $domainSuffixList = array_filter($domainSuffixList, function ($row) {
                $row = trim($row);
                if (!empty($row) && substr($row, 0, 2) != '//') {
                    return true;
                }
                return false;
            });
    
            return $domainSuffixList;
        }
    
        public function getMain(string $domain)
        {
            $domainList = $this->getDomainSuffix();
            $domainMap = array_flip($domainList);
            $origin = $domainSep = explode('.', $domain);
            $len = count($domainSep);
            for ($i = 0; $i < $len; $i++) {
                unset($domainSep[$i]);
                if (count($domainSep) == 0) {
                    break;
                }
                $s = implode('.', $domainSep);
                if (isset($domainMap[$s])) {
                    return '*.'.$origin[$i].'.'.$s;
                }
            }
    
            return $domain;
        }
    }
  • 相关阅读:
    linux启动流程
    控制nginx并发链接数量和客户端请求nginx的速率
    MySQL修改密码
    nginx站点目录及文件URL访问控制
    nginx日志相关优化安全
    根据参数优化nginx的服务性能
    nginx基本安全优化
    nginx rewrite
    nginx location
    nginx访问日志(access_log)
  • 原文地址:https://www.cnblogs.com/a-flydog/p/12145745.html
Copyright © 2020-2023  润新知