使用PHP确定URL是否为二级子目录


Determining whether URL is a second-level subdirectory using PHP

我只需要在我的网站上的二级子目录中显示某个图像。因此,如果我的URL是www.mywebsite.com/www.mywebsite.com/subdirectory1/,那么我不希望图像出现。

但是,如果我的URL是www.mywebsite.com/subdirectory1/subdirectory2/,那么我确实希望图像出现。

我该如何使用PHP来确定我是否在二级子目录中?我会使用is_dir吗?

if (count(explode("/",$_SERVER["SERVER_NAME"]))>=3) {
    echo "IM IN 2nd LEVEL";
} else {
    echo "IM ONLY A 1st LEVEL";
}

应该这样做——有点像黑客,但有效。

你可以做:

$path = parse_url($url, PHP_URL_PATH);
if($path){
  $elements = explode('/', trim($path, '/'));
  if(count($elements) > 1){
    // show the image...
  }
}

(PS:URL需要以一个方案开始,如http:////