DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

PHP - Change Active Directory Password

05.23.2007
| 34317 views |
  • submit to reddit
        
<?php
$username=$_POST['user_name'];
//print $username;

$ldap = ldap_connect($config['ldapServers'], 636);
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
  if ($ldap)
{
$bind = ldap_bind($ldap, $config['ldapUsername'], $config['ldapPassword']);
$filter="(sAMAccountName=$username)";
$result = ldap_search($ldap,"dc=yourcompany,dc=com",$filter);
//var_dump($results); 
        ldap_sort($ldap,$result,"sn");
       $info = ldap_get_entries($ldap, $result);
       for ($i=0; $i<$info["count"]; $i++)
       {
echo "<p>You are changing the password for <b> ". $info[$i]["givenname"][0] .", " . $info[$i]["sn"][0] ."</b> (" . $info[$i]["samaccountname"][0] .") to <b>" . $_POST['user_pass'] ."</b></p>\n";
             $passwd1 = $_POST['user_pass'];
              $userDn = $info[$i]["distinguishedname"][0];
              $newPassword = $passwd1;
      $newPassword = "\"" . $newPassword . "\"";
      $len = strlen($newPassword);
      for ($i = 0; $i < $len; $i++){
            $newPassw .= "{$newPassword{$i}}\000";}
      $newPassword = $newPassw;
      $userdata["unicodePwd"] = $newPassword;
      $result = ldap_mod_replace($ldap, $userDn , $userdata);
      if ($result) echo "Your password has been changed!" ;
      else echo "There was a problem changing your password, please call IT for help"; 
 }
         }
        @ldap_close($ldap);
?>