开发者

Detect if script in Domain Root or Subdirectory

I have written a small php script which uses clean urls for which mod_rewrite rules are used. that script needs to know where is located in order to run.

Currently to set path for the script. I use following two,

  • Case: root domain:

    define("ScriptURl",'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])); define("ScriptUri",dirname($_SERVER['PHP_SELF']));

  • Case: SubDir:

    define("ScriptUrls",'https://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/'); define("ScriptUrl",'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/'); define("ScriptUri",dirname($_SERVER['PHP_SELF']).'/')

Now, My question how can I detect is script is in subdir(yourdomain.com/subdir/) or root domain( yourdomain.com). Any way to do this in PHP or I have setup the config manually for each install?


Replace the variable $page_url with the url you are checking and replace the variable $sub_dir with the sub directory name such as '/subdir'

if ( FALSE === strpos ( strtolower ( $page_url ), strtolower ( $sub_dir ) ) )
{
    // run code for non sub dir
}
else
{
    // run code for SubDir
}

The strtolower() functions are not very necessary but I usually use those to make sure you are not worrying about case sensitive checking.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜