0

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:

5

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;
}
| improve this answer | |
3

Are you looking for http://api.drupal.org/api/drupal/includes--path.inc/function/arg ?

arg(0); // should be 'directory'

That's if you're referring to path components (eg "node" and "1" in "node/1") rather than actual directories, which are a filesystem component as kiamlaluno says. But if those were actual directories, then this wouldn't be a Drupal question :)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.