php中取得URL的根域名的代码


Posted in PHP onMarch 23, 2011
<?php 
/** 
* 取得根域名 
* 
* @author lonely 
* @create 2011-3-11 
* @version 0.1 
* @lastupdate lonely 
* @package Sl 
*/ 
class Sl_RootDomain{ 
private static $self; 
private $domain=null; 
private $host=null; 
private $state_domain; 
private $top_domain; 
/** 
* 取得域名分析实例 
* Enter description here ... 
*/ 
public static function instace(){ 
if(!self::$self) 
self::$self=new self(); 
return self::$self; 
} 
private function __construct(){ 
$this->state_domain=array( 
'al','dz','af','ar','ae','aw','om','az','eg','et','ie','ee','ad','ao','ai','ag','at','au','mo','bb','pg','bs','pk','py','ps','bh','pa','br','by','bm','bg','mp','bj','be','is','pr','ba','pl','bo','bz','bw','bt','bf','bi','bv','kp','gq','dk','de','tl','tp','tg','dm','do','ru','ec','er','fr','fo','pf','gf','tf','va','ph','fj','fi','cv','fk','gm','cg','cd','co','cr','gg','gd','gl','ge','cu','gp','gu','gy','kz','ht','kr','nl','an','hm','hn','ki','dj','kg','gn','gw','ca','gh','ga','kh','cz','zw','cm','qa','ky','km','ci','kw','cc','hr','ke','ck','lv','ls','la','lb','lt','lr','ly','li','re','lu','rw','ro','mg','im','mv','mt','mw','my','ml','mk','mh','mq','yt','mu','mr','us','um','as','vi','mn','ms','bd','pe','fm','mm','md','ma','mc','mz','mx','nr','np','ni','ne','ng','nu','no','nf','na','za','aq','gs','eu','pw','pn','pt','jp','se','ch','sv','ws','yu','sl','sn','cy','sc','sa','cx','st','sh','kn','lc','sm','pm','vc','lk','sk','si','sj','sz','sd','sr','sb','so','tj','tw','th','tz','to','tc','tt','tn','tv','tr','tm','tk','wf','vu','gt','ve','bn','ug','ua','uy','uz','es','eh','gr','hk','sg','nc','nz','hu','sy','jm','am','ac','ye','iq','ir','il','it','in','id','uk','vg','io','jo','vn','zm','je','td','gi','cl','cf','cn','yr' 
); 
$this->top_domain=array('com','arpa','edu','gov','int','mil','net','org','biz','info','pro','name','museum','coop','aero','xxx','idv','me','mobi'); 
$this->url=$_SERVER['HTTP_HOST']; 
} 
/** 
* 设置URL 
* Enter description here ... 
* @param string $url 
*/ 
public function setUrl($url=null){ 
$url=$url?$url:$this->url; 
if(empty($url))return $this; 
if(!preg_match("/^http::/is", $url)) 
$url="http://".$url; 
$url=parse_url(strtolower($url)); 
$urlarr=explode(".", $url['host']); 
$count=count($urlarr); 
if ($count<=2){ 
$this->domain=array_pop($url); 
}else if ($count>2){ 
$last=array_pop($urlarr); 
$last_1=array_pop($urlarr); 
if(in_array($last, $this->top_domain)){ 
$this->domain=$last_1.'.'.$last; 
$this->host=implode('.', $urlarr); 
}else if (in_array($last, $this->state_domain)){ 
$last_2=array_pop($urlarr); 
if(in_array($last_1, $this->top_domain)){ 
$this->domain=$last_2.'.'.$last_1.'.'.$last; 
$this->host=implode('.', $urlarr); 
}else{ 
$this->host=implode('.', $urlarr).$last_2; 
$this->domain=$last_1.'.'.$last; 
} 
} 
} 
return $this; 
} 
/** 
* 取得域名 
* Enter description here ... 
*/ 
public function getDomain(){ 
return $this->domain; 
} 
/** 
* 取得主机 
* Enter description here ... 
*/ 
public function getHost(){ 
return $this->host; 
} 
} 
?>
PHP 相关文章推荐
我的论坛源代码(十)
Oct 09 PHP
PHP MYSQL乱码问题,使用SET NAMES utf8校正
Nov 30 PHP
理解php原理的opcodes(操作码)
Oct 26 PHP
PHP调用Linux的命令行执行文件压缩命令
Jan 27 PHP
php+mysql删除指定编号员工信息的方法
Jan 14 PHP
php header函数的常用http头设置
Jun 25 PHP
php网页版聊天软件实现代码
Aug 12 PHP
PHP设计模式之单例模式原理与实现方法分析
Apr 25 PHP
PHP连接MySQL数据库并以json格式输出
May 21 PHP
ThinkPHP5框架实现简单的批量查询功能示例
Jun 07 PHP
PHP递归的三种常用方式
Feb 28 PHP
PHP代码加密的方法总结
Mar 13 PHP
PHP+JS+rsa数据加密传输实现代码
Mar 23 #PHP
PHP 事件机制(2)
Mar 23 #PHP
php函数之子字符串替换&amp;#65279; str_replace
Mar 23 #PHP
php expects parameter 1 to be resource, array given 错误
Mar 23 #PHP
php去掉字符串的最后一个字符附substr()的用法
Mar 23 #PHP
PHPUnit PHP测试框架安装方法
Mar 23 #PHP
开启CURL扩展,让服务器支持PHP curl函数(远程采集)
Mar 19 #PHP
You might like
用PHP实现小型站点广告管理(修正版)
2006/10/09 PHP
SWFUpload与CI不能正确上传识别文件MIME类型解决方法分享
2011/04/18 PHP
PHP中date与gmdate的区别及默认时区设置
2014/05/12 PHP
PHP中模拟处理HTTP PUT请求的例子
2014/07/22 PHP
PHP查询并删除数据库多列重复数据的方法(利用数组函数实现)
2016/02/23 PHP
PHP中header用法小结
2016/05/23 PHP
PHP回调函数概念与用法实例分析
2017/11/03 PHP
一次因composer错误使用引发的问题与解决
2019/03/06 PHP
PHP+fiddler抓包采集微信文章阅读数点赞数的思路详解
2019/12/20 PHP
dropdownlist之间的互相联动实现(显示与隐藏)
2009/11/24 Javascript
jQuery验证插件 Validate详解
2014/11/20 Javascript
jQuery中size()方法用法实例
2014/12/27 Javascript
JS判断元素是否在数组内的实现代码
2016/03/30 Javascript
基于jQuery实现的幻灯图片切换
2016/12/02 Javascript
jQuery插件HighCharts绘制2D柱状图、折线图和饼图的组合图效果示例【附demo源码下载】
2017/03/09 Javascript
jquery插件开发之选项卡制作详解
2017/08/30 jQuery
给vue项目添加ESLint的详细步骤
2017/09/29 Javascript
PYTHON正则表达式 re模块使用说明
2011/05/19 Python
python字符串对其居中显示的方法
2015/07/11 Python
Python自动发邮件脚本
2017/03/31 Python
Python基础教程之异常详解
2019/01/10 Python
python通过paramiko复制远程文件及文件目录到本地
2019/04/30 Python
用python打开摄像头并把图像传回qq邮箱(Pyinstaller打包)
2020/05/17 Python
HTML5 Canvas实现文本对齐的方法总结
2016/03/24 HTML / CSS
SheIn俄罗斯:时尚女装网上商店
2017/02/28 全球购物
Camper鞋西班牙官方网上商店:西班牙马略卡岛的鞋类品牌
2019/03/14 全球购物
Myprotein亚太地区:欧洲第一在线运动营养品牌
2020/12/20 全球购物
自我评价的范文
2014/02/02 职场文书
奥巴马英文演讲稿
2014/05/15 职场文书
医院竞聘演讲稿
2014/05/16 职场文书
年终考核实施方案
2014/05/26 职场文书
优秀班主任经验交流材料
2014/06/02 职场文书
开展创先争优活动总结
2014/08/28 职场文书
党的群众路线教育实践活动总结大会主持词
2014/10/30 职场文书
SpringBoot项目中控制台日志的保存配置操作
2021/06/18 Java/Android
python ConfigParser库的使用及遇到的坑
2022/02/12 Python