How do I dynamically change the $GLOBAL['base_path'] variable? Example scenario:

All authenticated users should see /members/ at the beginning of each URL path, and all anonymous users should see /guests/ at the beginning of each URL path.

So if an anonymous user were to visit example.com/node they'd be redirected to example.com/guests/node, and once they logged in they'd be redirected to example.com/members/node.

I'm sure this is not impossible because using a multilingual site allows for paths to be negotiated by prefix only, but I can't seem to figure out how to go about it.

Here are some of the things I've tried (some of which have been out of desparation):

Example 1:

function mymodule_init() {
  if (user_is_logged_in()) {
    $GLOBALS['base_path'] = '/members/';
  }
  else {
    $GLOBALS['base_path'] = '/guests/';  
  }
}


Example 2:

function mymodule_init() {
  $prefix_default = 'guests/';
  if (user_is_logged_in()) {
    $prefix_default = 'members/';
  }
  $get_q = isset($_GET['q']) ? $prefix_default . $_GET['q'] : $prefix_default;
  $args = explode('/', $get_q);
  $prefix = array_shift($args);
  $args_new = array();
  if (!empty($args)) {
    foreach ($args as $arg) {
      if ($arg != $prefix) {
        $args_new[] = $arg;
      }
    }
  }
  $GLOBALS['base_path'] = '/' . $prefix_default;
  $_GET['q'] = implode('/', $args_new);
}


Example 3:

function custom_url_rewrite_inbound(&amp;$result, $path, $path_language) {
  global $user;
  if (!$user-&gt;uid || !isset($user-&gt;status) || empty($user-&gt;status)) {
    // Allow access to front page only, or guests/* paths, or even user/* paths
    if (($path != 'node') &amp;&amp; ((!preg_match('|^guests([/]?.*)|', $path)) &amp;&amp; (!preg_match('|^user([/]?.*)|', $path)))) {
      $result = 'user';
    }
  }
}


Any input would be most appreciated. Thanks!
I don't think base path is what you need here.
Once the user's page is loading it is too late to change their URL, unless you redirect them them to the /members or /guests url with drupal_goto().

Base path is also not what you need because base path is telling drupal where your files are in the web root, so changing the $base_path global is likely just going to break your site unless your files are in /var/www/yoursite/members and /var/www/yoursite/guests (I'm assuming your web server setup here for the paths).

The main thing here is that you need to make sure your users are actually hitting the paths www.example.com/members/path and www.example.com/guests/path and then you have to make drupal handle those paths appropriately or you will just get page not found errors.

This is how the drupal multilingual (locale) system works.
It rewrites urls that appear on the page to point to www.example.com/langcode/path so the user is actually navigating to those pages. If there is no langcode part drupal just assumes the default language.

In drupal 6 this is ingrained into the url() function, which calls language_url_rewrite().
In drupal 7 the local module has an implementation of hook_url_outbound_ater() (local_url_outbound_alter()).

You could replicate this by implementing custom_url_rewrite_outbound() (read the docs for that, it is not a normal hook).
Or use the url alter module and implement its hook_url_outbound_alter() hook.

That's not all though, because users are going to paths like www.example.com/langcode/path those paths have to be valid drupal menu system paths or else drupal won't know that functions to call for that given page.

For the multilingual system drupal core handles this in the bootstrap phase, before any menu handling has happened.

See _drupal_bootstrap(), which for the DRUPAL_BOOTSTRAP_LANGUAGE phase calls drupal_init_language(), which in turn calls language_initialize().

language_initialize() has code in it that detects the language from the url and then removes that language portion of the url from the $_GET['q'] variable so everything from then on just sees www.example.com/path and the page loads as normal.

If you were to use this same approach for your path then the system would try to load the menu item for www.example.com/members/path and it would likely not exist and you would get page not found errors.

However you could the also add an implementation of hook_boot() that does something similar to language_initialize() and save a global variable of your user type (member or guest) and then removes that part of the url from $_GET['q'] before the menu handler runs.

From then the $_GET['q'] variable doesn't have your user type anymore but you will be able to use the user type global you created from your other custom code if you need to.

This system relies on there being a default user type for when guests or members are not present (which can easily happen, for example the user can just type in the url manually instead of clicking one of your altered links).

So your system would have to assume the user type or work it out in some other way (easy because you have user_is_logged_in() or global $user to determine the user type). It just means in these cases the user doesn't get to see that url scheme you want with the user type in it.

As you can see this is a complex requirement and the solution doesn't have 100% coverage so i it was me I would reconsider doing this at all.

Alternatively you may be able to just have a members area, full of pages like views and nodes that do really have urls like /members/something, but not change the url for the whole site (although I'm just making assumptions on your requirements now).