dsfgdsjfd78773 2014-02-14 20:36
浏览 14
已采纳

如果正斜杠后没有任何内容,PHP获取当前目录

I have a site that uses ad banner rotators and I'm assigning them to different sections of a site depending on the current directory of the URL. BUT, if you're in the "root" of the site, meaning, the home page "www.somedomain.com" and there's no other directory, I want to assign it as such.

Right now, the only thing I can think of is this:

$SiteDirectory = explode('/',$url);

Which returns current directory after the last "/" of a URL.

But what if there's nothing after it? How do I identify if it is the root of the site?

This:

$Site = parse_url($url);

just gets me the domain, but if there's directories after it, it's useless to me.

And this:

$CurrentURL = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

gives me the the WHOLE thing....

So, how do I find out if it's the home directory?

  • 写回答

1条回答 默认 最新

  • doy2255 2014-02-14 20:54
    关注

    if there is something after '/' than $SiteDirectory array count must be greater than one as $SiteDirectory[0] contain domain name and indexes after that having sub directories

    $SiteDirectory = array_filter(explode('/',$url));
    if(0 == count($SiteDirectory)){
       // something is wrong
    }else{
        if(1 == count($SiteDirectory))
        {
            // you are on the root
        }else{
            $last_directory = $SiteDirectory[count($SiteDirectory) - 1];
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥20 关于变压器的具体案例分析
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式