Never been to DZone Snippets before?

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

About this user

Amanda Emily

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

PHP - Change Active Directory Password

   1  
   2  <?php
   3  $username=$_POST['user_name'];
   4  //print $username;
   5  
   6  $ldap = ldap_connect($config['ldapServers'], 636);
   7  ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
   8  ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
   9    if ($ldap)
  10  {
  11  $bind = ldap_bind($ldap, $config['ldapUsername'], $config['ldapPassword']);
  12  $filter="(sAMAccountName=$username)";
  13  $result = ldap_search($ldap,"dc=yourcompany,dc=com",$filter);
  14  //var_dump($results); 
  15          ldap_sort($ldap,$result,"sn");
  16         $info = ldap_get_entries($ldap, $result);
  17         for ($i=0; $i<$info["count"]; $i++)
  18         {
  19  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";
  20               $passwd1 = $_POST['user_pass'];
  21                $userDn = $info[$i]["distinguishedname"][0];
  22                $newPassword = $passwd1;
  23        $newPassword = "\"" . $newPassword . "\"";
  24        $len = strlen($newPassword);
  25        for ($i = 0; $i < $len; $i++){
  26              $newPassw .= "{$newPassword{$i}}\000";}
  27        $newPassword = $newPassw;
  28        $userdata["unicodePwd"] = $newPassword;
  29        $result = ldap_mod_replace($ldap, $userDn , $userdata);
  30        if ($result) echo "Your password has been changed!" ;
  31        else echo "There was a problem changing your password, please call IT for help"; 
  32   }
  33           }
  34          @ldap_close($ldap);
  35  ?>
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS