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 相关文章推荐
汉字转化为拼音(php版)
Oct 09 PHP
功能齐全的PHP发送邮件类代码附详细说明
Jul 10 PHP
php5新改动之短标记启用方法
Sep 11 PHP
PHP实现生成透明背景的PNG缩略图函数分享
Jul 08 PHP
php使用ob_start()实现图片存入变量的方法
Nov 14 PHP
ThinkPHP、ZF2、Yaf、Laravel框架路由大比拼
Mar 25 PHP
分享PHP-pcntl 实现多进程代码
Sep 30 PHP
PHP实现的策略模式简单示例
Aug 25 PHP
php制作圆形用户头像的实例_自定义封装类源代码
Sep 18 PHP
swoole_process实现进程池的方法示例
Oct 29 PHP
PHP实现字符串大小写转函数的功能实例
Feb 06 PHP
Yii 实现数据加密和解密
Mar 09 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
基于文本的搜索
2006/10/09 PHP
thinkphp的静态缓存用法分析
2014/11/29 PHP
使用 PHPStorm 开发 Laravel
2015/03/24 PHP
PHP中strnatcmp()函数“自然排序算法”进行字符串比较用法分析(对比strcmp函数)
2016/01/07 PHP
PHP的PDO错误与错误处理
2019/01/27 PHP
yii 框架实现按天,月,年,自定义时间段统计数据的方法分析
2020/04/04 PHP
浅析Prototype的模板类 Template
2011/12/07 Javascript
javascript中将Object转换为String函数代码 (json str)
2012/04/29 Javascript
基于pthread_create,readlink,getpid等函数的学习与总结
2013/07/17 Javascript
js 弹出框只弹一次(二次修改之后的)
2013/11/26 Javascript
Jquery遍历checkbox获取选中项value值的方法
2014/02/13 Javascript
详解AngularJS中的作用域
2015/06/17 Javascript
javascript中caller和callee详解
2015/08/10 Javascript
jquery ajax双击div可直接修改div中的内容
2016/03/04 Javascript
基于MVC5和Bootstrap的jQuery TreeView树形控件(二)之数据支持json字符串、list集合
2016/08/11 Javascript
jquery实现全选、不选、反选的两种方法
2016/09/06 Javascript
提高Web性能的前端优化技巧总结
2017/02/27 Javascript
Vue表单类的父子组件数据传递示例
2018/05/03 Javascript
Vue的路由及路由钩子函数的实现
2019/07/02 Javascript
老生常谈Python进阶之装饰器
2017/05/11 Python
详解python中asyncio模块
2018/03/03 Python
python3利用tcp实现文件夹远程传输
2018/07/28 Python
Python函数any()和all()的用法及区别介绍
2018/09/14 Python
Python基于matplotlib画箱体图检验异常值操作示例【附xls数据文件下载】
2019/01/07 Python
详解Python3中ceil()函数用法
2019/02/19 Python
简单了解python的一些位运算技巧
2019/07/13 Python
ubuntu上安装python的实例方法
2019/09/30 Python
matplotlib 对坐标的控制,加图例注释的操作
2020/04/17 Python
tensorflow基于CNN实战mnist手写识别(小白必看)
2020/07/20 Python
pycharm2020.1.2永久破解激活教程,实测有效
2020/10/29 Python
使用python操作lmdb对数据读取的实例
2020/12/11 Python
鱼油专家:Omegavia
2016/10/10 全球购物
生产部统计员岗位职责
2014/01/05 职场文书
升职自我推荐信范文
2015/03/25 职场文书
银行大堂经理培训心得体会
2016/01/09 职场文书
比较几种Redis集群方案
2021/06/21 Redis