diff --git a/CHANGES.md b/CHANGES.md
index 9515e3236d5b0a825b2eacd23309e14705aaea8d..c84cedc6feaf5aeca2ce8ca3c2fea5bca0b45673 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -6,6 +6,7 @@ Changes
 
 ### Unreleased
 
+* 2019-01-29 - Adopt code changes Moodle 3.5 core auth_ldap (MDL-63887).
 * 2018-12-05 - Changed travis.yml due to upstream changes.
 
 ### v3.5-r1
diff --git a/auth.php b/auth.php
index 6de69537a1f2e51ddc8e4c6faf000994bd0e359a..f618c6081bc58ba1febb0d2035fb7ca07cbc8585 100644
--- a/auth.php
+++ b/auth.php
@@ -102,8 +102,8 @@ class auth_plugin_ldap_syncplus extends auth_plugin_ldap {
             array_push($contexts, $this->config->create_context);
         }
 
-        $ldap_pagedresults = ldap_paged_results_supported($this->config->ldap_version, $ldapconnection);
-        $ldap_cookie = '';
+        $ldappagedresults = ldap_paged_results_supported($this->config->ldap_version, $ldapconnection);
+        $ldapcookie = '';
         foreach ($contexts as $context) {
             $context = trim($context);
             if (empty($context)) {
@@ -111,23 +111,28 @@ class auth_plugin_ldap_syncplus extends auth_plugin_ldap {
             }
 
             do {
-                if ($ldap_pagedresults) {
-                    ldap_control_paged_result($ldapconnection, $this->config->pagesize, true, $ldap_cookie);
+                if ($ldappagedresults) {
+                    ldap_control_paged_result($ldapconnection, $this->config->pagesize, true, $ldapcookie);
                 }
                 if ($this->config->search_sub) {
                     // Use ldap_search to find first user from subtree.
-                    $ldap_result = ldap_search($ldapconnection, $context, $filter, array($this->config->user_attribute));
+                    $ldapresult = ldap_search($ldapconnection, $context, $filter, array($this->config->user_attribute));
                 } else {
                     // Search only in this context.
-                    $ldap_result = ldap_list($ldapconnection, $context, $filter, array($this->config->user_attribute));
+                    $ldapresult = ldap_list($ldapconnection, $context, $filter, array($this->config->user_attribute));
                 }
-                if(!$ldap_result) {
+                if(!$ldapresult) {
                     continue;
                 }
-                if ($ldap_pagedresults) {
-                    ldap_control_paged_result_response($ldapconnection, $ldap_result, $ldap_cookie);
+                if ($ldappagedresults) {
+                    $pagedresp = ldap_control_paged_result_response($ldapconnection, $ldapresult, $ldapcookie);
+                    // Function ldap_control_paged_result_response() does not overwrite $ldapcookie if it fails, by
+                    // setting this to null we avoid an infinite loop.
+                    if ($pagedresp === false) {
+                        $ldapcookie = null;
+                    }
                 }
-                if ($entry = @ldap_first_entry($ldapconnection, $ldap_result)) {
+                if ($entry = @ldap_first_entry($ldapconnection, $ldapresult)) {
                     do {
                         $value = ldap_get_values_len($ldapconnection, $entry, $this->config->user_attribute);
                         $value = core_text::convert($value[0], $this->config->ldapencoding, 'utf-8');
@@ -135,13 +140,13 @@ class auth_plugin_ldap_syncplus extends auth_plugin_ldap {
                         $this->ldap_bulk_insert($value);
                     } while ($entry = ldap_next_entry($ldapconnection, $entry));
                 }
-                unset($ldap_result); // Free mem.
-            } while ($ldap_pagedresults && $ldap_cookie !== null && $ldap_cookie != '');
+                unset($ldapresult); // Free mem.
+            } while ($ldappagedresults && $ldapcookie !== null && $ldapcookie != '');
         }
 
         // If LDAP paged results were used, the current connection must be completely
         // closed and a new one created, to work without paged results from here on.
-        if ($ldap_pagedresults) {
+        if ($ldappagedresults) {
             $this->ldap_close(true);
             $ldapconnection = $this->ldap_connect();
         }
@@ -152,7 +157,9 @@ class auth_plugin_ldap_syncplus extends auth_plugin_ldap {
         $count = $DB->count_records_sql('SELECT COUNT(username) AS count, 1 FROM {tmp_extuser}');
         if ($count < 1) {
             mtrace(get_string('didntgetusersfromldap', 'auth_ldap'));
-            exit;
+            $dbman->drop_table($table);
+            $this->ldap_close();
+            return false;
         } else {
             mtrace(get_string('gotcountrecordsfromldap', 'auth_ldap', $count));
         }