views:

30

answers:

1

Hi, 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.

  • root 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?

A: 

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.

Will Ayers
I am not checking for any URL. I have to set the path in a config file. Currently, I manually choose Case: root domain, if script is installed as yourdomain.com or Case: SubDir: yourdomain.com/subdir then further I define all paths relative to ScriptURl path. I need some need a method what would automatically detect this. Hope I am clear this time
Gaurish
So maybe just a switch case statement?
Will Ayers