找回密码
 注册会员

QQ登录

只需一步,快速开始

微信登录

微信扫一扫,快速登录

查看: 518|回复: 0

[WordPress] PHP获取URL地址中的一级域名、顶级域名的方法

[复制链接]

2

主题

0

回帖

6

积分

潜水

积分
6
发表于 2024-3-5 01:03:24 | 显示全部楼层 |阅读模式
在WordPress主题开发中经常需要获取url地址中的一级域名或者顶级域名,特别是在需要验证权限时这个功能就非常重要,虽然php内置了很多超全局变量,但是不能让我们轻易的获取到顶级域名,下面我们可以通过自定义函数的方式来截取到我们需要的。
  1. /**
  2. * 获取顶级域名
  3. *
  4. * @param string $url 域名 HOST,如:www.paopaowo.com ,www.baidu.com
  5. * @return string 顶级域名 如:paopaowo.cn ,baidu.com
  6. */
  7. function url_root($url=""){
  8.     $url = $url ? $url : $_SERVER["HTTP_HOST"];
  9.     $dual_host = array('aaa.pro','ac.cn','ac.kr','ac.mu','aca.pro','acct.pro','ae.org','ah.cn','ar.com','avocat.pro','bar.pro','biz.ki','biz.pl','bj.cn','br.com','busan.kr','chungbuk.kr','chungnam.kr','club.tw','cn.com','co.ag','co.am','co.at','co.bz','co.cm','co.com','co.gg','co.gl','co.gy','co.il','co.im','co.in','co.je','co.kr','co.lc','co.mg','co.ms','co.mu','co.nl','co.nz','co.uk','co.ve','co.za','com.af','com.ag','com.am','com.ar','com.au','com.br','com.bz','com.cm','com.cn','com.co','com.de','com.ec','com.es','com.gl','com.gr','com.gy','com.hn','com.ht','com.im','com.ki','com.lc','com.lv','com.mg','com.ms','com.mu','com.mx','com.nf','com.pe','com.ph','com.pk','com.pl','com.ps','com.pt','com.ro','com.ru','com.sb','com.sc','com.se','com.sg','com.so','com.tw','com.vc','com.ve','cpa.pro','cq.cn','daegu.kr','daejeon.kr','de.com','ebiz.tw','edu.cn','edu.gl','eng.pro','es.kr','eu.com','fin.ec','firm.in','fj.cn','game.tw','gangwon.kr','gb.com','gb.net','gd.cn','gen.in','go.kr','gov.cn','gr.com','gs.cn','gwangju.kr','gx.cn','gyeongbuk.kr','gyeonggi.kr','gyeongnam.kr','gz.cn','ha.cn','hb.cn','he.cn','hi.cn','hk.cn','hl.cn','hn.cn','hs.kr','hu.com','hu.net','idv.tw','in.net','incheon.kr','ind.in','info.ec','info.ht','info.ki','info.nf','info.pl','info.ve','jeju.kr','jeonbuk.kr','jeonnam.kr','jl.cn','jp.net','jpn.com','js.cn','jur.pro','jx.cn','kg.kr','kiwi.nz','kr.com','law.pro','ln.cn','me.uk','med.ec','med.pro','mex.com','mo.cn','ms.kr','ne.kr','net.af','net.ag','net.am','net.br','net.bz','net.cm','net.cn','net.co','net.ec','net.gg','net.gl','net.gr','net.gy','net.hn','net.ht','net.im','net.in','net.je','net.ki','net.lc','net.lv','net.mg','net.mu','net.my','net.nf','net.nz','net.ph','net.pk','net.pl','net.ps','net.ru','net.sb','net.sc','net.so','net.vc','net.ve','nm.cn','no.com','nom.ag','nom.co','nom.es','nom.ro','nx.cn','or.at','or.jp','or.kr','or.mu','org.af','org.ag','org.am','org.bz','org.cn','org.es','org.gg','org.gl','org.gr','org.hn','org.ht','org.il','org.im','org.in','org.je','org.ki','org.lc','org.lv','org.mg','org.ms','org.mu','org.my','org.nz','org.pk','org.pl','org.ps','org.ro','org.ru','org.sb','org.sc','org.so','org.uk','org.vc','org.ve','pe.kr','pro.ec','qc.com','qh.cn','radio.am','radio.fm','re.kr','recht.pro','ru.com','sa.com','sc.cn','sc.kr','sd.cn','se.com','senet','seoul.kr','sh.cn','sn.cn','sx.cn','tj.cn','tw.cn','uk.com','uk.net','ulsan.kr','us.com','us.org','uy.com','web.ve','xj.cn','xz.cn','yn.cn','za.com','zj.cn');
  10.     $url_arr   = explode(".", $url);
  11.     if (count($url_arr) <= 2) {
  12.         $host = $url;
  13.     } else {
  14.         $last   = array_pop($url_arr);
  15.         $last_1 = array_pop($url_arr);
  16.         $last_2 = array_pop($url_arr);

  17.         $host   = $last_1.'.'.$last;
  18.         if (in_array($host, $dual_host)) {
  19.             $host = $last_2.'.'.$last_1.'.'.$last;
  20.         }
  21.     }
  22.     return $host;
  23. }
复制代码




您需要登录后才可以回帖 登录 | 注册会员 微信登录

本版积分规则

小黑屋|简约版|网站地图|泡泡窝 ( 粤ICP备2023154091号-1|粤公网安备44098102660676号 )

GMT+8, 2024-6-19 21:56

Powered by Discuz! X3.5

Copyright © 2023-2024 Paopaowo.Com All Rights Reserved.

快速回复 返回顶部 返回列表