How to detect if a user is in a specific directory/accessing a specific path? For example I need to know if a user is in this directory http://example.com/directory; the value should return true if the use is on any of the following pages:


http://example.com/directory/cars
http://example.com/directory/homes
http://example.com/directory/homes/howtosell
http://example.com/directory/food/seafood
http://example.com/directory/food/seafood/fish
For example I need to know if a user is in this directory http://example.com/directory


If "directory" is a filesystem directory, then it's not possible for a Drupal function to know when a user is visiting that directory, for the simple fact Drupal is not invoked to serve the content of a directory. One of the Rewrite directives present in the .htaccess file that comes with Drupal is the following one:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]


If "directory" is the path for which a module defines a menu callback, and there isn't a directory with that name, then it is possible for a function to return TRUE when the user is visiting a specific path.

function check_path(array $directories) {
  $path = $_GET['q'];

  foreach ($directories as $dir) {
    if ($dir == $path) {
      return TRUE;
    }
  }

  return FALSE;
}


This function doesn't consider the path aliases. If http://example.com/path-alias is the path alias for http://example.com/real-path, $_GET['q'] will contain real-path when the user visits http://example.com/path-alias; the same is true for arg(), which will return an array containing real-path.
If the function needs to consider also the path aliases, then it needs to be rewritten as:

function check_path(array $directories) {
  $path = $_GET['q'];

  foreach ($directories as $dir) {
    if (drupal_get_normal_path($dir) == $path) {
      return TRUE;
    }
  }

  return FALSE;
}