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.
- ro开发者_如何学JAVAot domain : yourdomain.com
- sub dir yourdomain.com/subdir/
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.
精彩评论