diff --git includes/common.inc includes/common.inc
index 5a204bd..e762728 100644
--- includes/common.inc
+++ includes/common.inc
@@ -212,26 +212,27 @@ function drupal_get_region_content($region = NULL, $delimiter = ' ') {
 /**
  * Get the name of the currently active install profile.
  *
- * When this function is called during Drupal's initial installation process,
- * the name of the profile that's about to be installed is stored in the global
- * installation state. At all other times, the standard Drupal systems variable
- * table contains the name of the current profile, and we can call variable_get()
- * to determine what one is active.
+ * This value is stored in the settings.php file generated during installation.
+ *
+ * Because we need to have this information before the database connection is initialized,
+ * to correctly determine where to search for modules and themes, we can not use the variable
+ * system to store this value.
+ *
+ * If this function is called during the initial installation process, the global
+ * variable will be initialized to the profile requested by the user and then
+ * stored in the settings file for future use.
  *
  * @return $profile
  *   The name of the install profile.
  */
 function drupal_get_profile() {
-  global $install_state;
+  global $installed_profile;
 
-  if (isset($install_state['parameters']['profile'])) {
-    $profile = $install_state['parameters']['profile'];
-  }
-  else {
-    $profile = variable_get('install_profile', 'standard');
+  if (!isset($installed_profile)) {
+    $installed_profile = 'standard';
   }
 
-  return $profile;
+  return $installed_profile;
 }
 
 
diff --git includes/update.inc includes/update.inc
index 56bbe5b..881510b 100644
--- includes/update.inc
+++ includes/update.inc
@@ -72,11 +72,18 @@ function update_prepare_d7_bootstrap() {
   // still being used.
   include_once DRUPAL_ROOT . '/includes/install.inc';
   drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION);
-  global $databases, $db_url, $update_rewrite_settings;
+  global $databases, $db_url, $update_rewrite_database, $update_rewrite_profile, $installed_profile;
   if (empty($databases) && !empty($db_url)) {
     $databases = update_parse_db_url($db_url);
     // Record the fact that the settings.php file will need to be rewritten.
-    $update_rewrite_settings = TRUE;
+    $update_rewrite_database = TRUE;
+  }
+
+  if (empty($installed_profile)) {
+    $update_rewrite_profile = TRUE;
+  }
+
+  if ($update_rewrite_profile || $update_rewrite_database) {
     $settings_file = conf_path() . '/settings.php';
     $writable = drupal_verify_install_file($settings_file, FILE_EXIST|FILE_READABLE|FILE_WRITABLE);
     $requirements = array(
@@ -169,11 +176,32 @@ function update_fix_d7_requirements() {
 
   // Rewrite the settings.php file if necessary, see
   // update_prepare_d7_bootstrap().
-  global $update_rewrite_settings, $db_url;
-  if (!empty($update_rewrite_settings)) {
+  global $update_rewrite_database, $update_rewrite_profile, $db_url, $installed_profile;
+  if (!empty($update_rewrite_database)) {
     $databases = update_parse_db_url($db_url);
     file_put_contents(conf_path() . '/settings.php', "\n" . '$databases = ' . var_export($databases, TRUE) . ';', FILE_APPEND);
   }
+
+  if (!empty($update_rewrite_profile)) {
+    // The install profile was renamed from 'default' to 'standard'.
+    // This change was originally handled in system_update_7049(), 
+    // but because we no longer use a variable to store the profile name,
+    // it is no longer necessary.
+    $profile = variable_get('install_profile', 'standard');
+    if ($profile == 'default') {
+      $profile = 'standard';
+    }
+
+    file_put_contents(conf_path() . '/settings.php', "\n" . '$installed_profile = ' . $profile . ';', FILE_APPEND);
+
+    // We modify the installed_profile global at, so that future calls to drupal_get_profile() will have return the correct value, 
+    // because the settings.php file has already been loaded at this point.
+    $installed_profile = $profile;
+
+    // Remove the uneccesary variable.
+    variable_del('install_profile');
+  }
+
   if (drupal_get_installed_schema_version('system') < 7000 && !variable_get('update_d7_requirements', FALSE)) {
     // Add the cache_path table.
     $schema['cache_path'] = drupal_get_schema_unprocessed('system', 'cache');
diff --git install.php install.php
index 223e873..33a9da4 100644
--- install.php
+++ install.php
@@ -1002,6 +1002,15 @@ function install_settings_form_submit($form, &$form_state) {
     'value'    => $form_state['values']['db_prefix'],
     'required' => TRUE,
   );
+
+  // The $_GET['profile'] here has been validated against a file_exists() call and filtered
+  // to remove possible exploits. 
+  // It will not allow an attacker to write malicious values into the config file.
+  $settings['installed_profile'] = array(
+    'value' => $_GET['profile'],
+    'required' => TRUE
+  );
+
   drupal_rewrite_settings($settings);
   // Indicate that the settings file has been verified, and check the database
   // for the last completed task, now that we have a valid connection. This
@@ -1475,9 +1484,6 @@ function install_finished(&$install_state) {
   // Register actions declared by any modules.
   actions_synchronize();
 
-  // Remember the profile which was used.
-  variable_set('install_profile', drupal_get_profile());
-
   // Install profiles are always loaded last
   db_update('system')
     ->fields(array('weight' => 1000))
diff --git modules/simpletest/drupal_web_test_case.php modules/simpletest/drupal_web_test_case.php
index 39e553f..5e9a3e0 100644
--- modules/simpletest/drupal_web_test_case.php
+++ modules/simpletest/drupal_web_test_case.php
@@ -564,7 +564,7 @@ class DrupalUnitTestCase extends DrupalTestCase {
   }
 
   protected function setUp() {
-    global $db_prefix, $conf;
+    global $db_prefix, $conf, $installed_profile;
 
     // Store necessary current values before switching to prefixed database.
     $this->originalPrefix = $db_prefix;
@@ -1093,7 +1093,7 @@ class DrupalWebTestCase extends DrupalTestCase {
    *   List of modules to enable for the duration of the test.
    */
   protected function setUp() {
-    global $db_prefix, $user, $language;
+    global $db_prefix, $user, $language, $installed_profile;
 
     // Store necessary current values before switching to prefixed database.
     $this->originalLanguage = $language;
@@ -1137,7 +1137,7 @@ class DrupalWebTestCase extends DrupalTestCase {
     $this->preloadRegistry();
 
     // Include the default profile
-    variable_set('install_profile', 'standard');
+    $installed_profile = 'standard';
     $profile_details = install_profile_info('standard', 'en');
 
     // Install the modules specified by the default profile.
@@ -1224,7 +1224,7 @@ class DrupalWebTestCase extends DrupalTestCase {
    * and reset the database prefix.
    */
   protected function tearDown() {
-    global $db_prefix, $user, $language;
+    global $db_prefix, $user, $language, $installed_profile;
 
     // In case a fatal error occured that was not in the test process read the
     // log to pick up any fatal errors.
@@ -1253,6 +1253,9 @@ class DrupalWebTestCase extends DrupalTestCase {
       // Return the database prefix to the original.
       $db_prefix = $this->originalPrefix;
 
+      // Return the original install profile.
+      $installed_profile = $this->originalProfile;
+
       // Return the user to the original one.
       $user = $this->originalUser;
       drupal_save_session(TRUE);
diff --git modules/system/system.install modules/system/system.install
index d1559c3..4c397ed 100644
--- modules/system/system.install
+++ modules/system/system.install
@@ -2657,9 +2657,10 @@ function system_update_7048() {
  * Rename 'Default' profile to 'Standard.'
  */
 function system_update_7049() {
-  if (variable_get('install_profile', 'standard') == 'default') {
-    variable_set('install_profile', 'standard');
-  }
+  // This change is now handled by update_fix_d7_requirements().
+  // We no longer store the install profile in a variable, but rather
+  // keep it in the settings.php, as it may need to be accessed
+  // before the database connection has been made.
 }
 
 /**
diff --git sites/default/default.settings.php sites/default/default.settings.php
old mode 100644
new mode 100755
index 52779e6..5286bc0
--- sites/default/default.settings.php
+++ sites/default/default.settings.php
@@ -154,6 +154,17 @@
 $databases = array();
 $db_prefix = '';
 
+
+/**
+* Install profile
+* 
+* Drupal can be installed with one of several install profiles, which
+* will provide some initial configuration and an additional 'search path'
+* for modules and themes to be detected.
+*/
+$installed_profile = '';
+
+
 /**
  * Access control for update.php script
  *
