title: Change AD Password using PHP/COM/ADSI/LDAP
body: <p>I've been stumped on this for a couple days now.  I've tried various solutions to no avail.  Please help...</p>
  
  <p><strong>Problem:</strong>  We have two domain controller, which are not under our management.  We are able to connect via LDAP on port 389, but are not able to connect securely over port 636.</p>
  
  <p>We are developing a system to allow a number of self-service facilities, one of which is a password recovery tool.  This works up to the point of resetting the users password.</p>
  
  <p>I have found some code <a href="http://php.net/manual/en/function.ldap-mod-replace.php#70136" rel="nofollow">via the PHP manual</a> which appears to do what we need, but cant seem to get it to work.</p>
  
  <p>This is the code I have thus far</p>
  
  <pre><code>if ($caller==="change"){
  if (($newPword1 === NULL)||($newPword1 === "" )){ return false;}
  if (($newPword2 === NULL)||($newPword2 === "" )){ return false;}
  if ($newPword1 != $newPword2) {
    $result["ERROR"]="1";
    $result["DETAILS"]="Your new password and the confirmation must match!";
    exit();
  }
  try { 
      $adldap = new adLDAP(); 
  } catch (adLDAPException $e) {
      $result["ERROR"]="1";
      $result["DETAILS"]="An error occurred in adLDAP";
      echo json_encode($result);
      exit();
  }
  
  $userinfo = $adldap-&gt;user()-&gt;info($username, array("givenname","dn","lockouttime"));
  $res = $userinfo[0]["lockouttime"];
  $userDN = $userinfo[0]["dn"];
  $firstName = $userinfo[0]["givenname"];
  $authUser = $adldap-&gt;authenticate($username,$currentPword);
  if ($authUser){
      try {
          $adminUsername = $domain."\\".$adminUsername;
          $srvDN = "LDAP://".$server."/";
  
          try {
              $ADSI = new COM("LDAP:");
          } catch (exception $e){
              $result["ERROR"]="1";
              $result["ERRORmsg"]=$e-&gt;getMessage();
              echo json_encode($result);
              exit();
          }
          try {
              $user = $ADSI-&gt;OpenDSObject($srvDN.$userDN, $adminUsername, $adminPassword, 1);
          } catch (exception $e){
              $result["ERROR"]="2";
              $result["ERRORmsg"]= $e-&gt;getMessage();
              echo json_encode($result);
              exit();
          }
          try { //set password
              if ($user){
                  $result["object"]="Success";
              } else {
                  $result["object"]="Failed";
              }
              $user-&gt;SetPassword($newPword1);  //line:114 -&gt; error occurring on this line
              $user-&gt;SetInfo();
              $result["ERROR"]="0";
              $result["DETAILS"]="Thank you $firstName[0]&lt;br&gt;&lt;strong&gt;Your password has been changed&lt;/strong&gt;&lt;br&gt;&lt;br&gt;This may take up to 30 minutes to take effect depending on your location";
          } catch (exception $e) {
              $result["ERROR"]="3";
              $result["ERRORmsg"]=$e." - ".$e-&gt;getMessage();
              $result["DETAILS"]="An Error Occurred.";
          }
          unset($user);
          unset($ADSI);
      } catch (exception $e){
          $result["ERROR"]="1";
          $result["DETAILS"]="An Error Occurred in the ADSI COM";
          echo json_encode($result);
          exit();
      }
  } else {
      if ($res[0] != "0"){
          $result["ERROR"]="1";
          $result["DETAILS"]="Im sorry $firstName[0].&lt;br&gt;Your account is now locked.  Please contact the IT Service Desk for advice";
      } else {
          $result["ERROR"]="1";
          $result["DETAILS"]="Im sorry $firstName[0].&lt;br&gt;Your current password is incorrect";
      }
  }
  </code></pre>
  
  <p>In testing <code>$result["object"]</code> returns "Success".  But the code seems to fail on the <code>$user-&gt;SetPassword($newPword1);</code> line.</p>
  
  <p>The error that is being returned is:</p>
  
  <pre><code>ERROR -&gt; "3"
  object -&gt; "Success"
  ERRORmsg -&gt; "exception 'com_exception' with message '&lt;b&gt;Source:&lt;/b&gt; Unknown&lt;br/&gt;&lt;b&gt;Description:&lt;/b&gt; Unknown' in C:\inetpub\wwwroot\&lt;path&gt;\&lt;filename&gt;.php:114
  Stack trace:
  #0 C:\inetpub\wwwroot\&lt;path&gt;\&lt;filename&gt;.php(114): variant-&gt;SetPassword('P@ssw0rd')
  #1 {main} - &lt;b&gt;Source:&lt;/b&gt; Unknown&lt;br/&gt;&lt;b&gt;Description:&lt;/b&gt; Unknown"
  DETAILS -&gt; "An Error Occurred."
  </code></pre>
  
  <p>The above code is in a php doc on an IIS Web Server which is called by a user viewable page over https</p>
  
  <p>Are you able to offer any advice or guidance?</p>