Mark Bogner
Mark Bogner

Reputation: 471

PHP Getting Current Directory if nothing exists after the forward slash

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?

Upvotes: 0

Views: 143

Answers (1)

Hassan
Hassan

Reputation: 742

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];
    }
}

Upvotes: 1

Related Questions