使用php

时间:2015-10-05 06:55:59

标签: php ldap phpldapadmin

我正在尝试使用php通过以下代码在Active Directory中创建新用户:     

$server = "ldaps://172.16.0.2";
$user = 'someuser'.'@domain.com'; //admin user
$psw = "somepassword"; //admin password

echo "<h2>Mag dagdag ako ng bagong impormasyon</h2>";

ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);

$ds =ldap_connect($server,389); 

 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
 ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);

$r=ldap_bind($ds, $user, $psw); 


$info['cn']='trying.hard';
$info['sAMAccountName']='trying.hard';

$info['givenName']='trying';

$info['mail']='trying.hard@domain.com';
$info['sn']='hard';

$dn='OU=CSS,OU=ETC,OU=AdminStaff,OU=MainBldg,OU=CollegeStaff,OU=DoMain,DC=domain,DC=com ';  

$info['objectCategory'] = 'CN=Person,CN=Schema,CN=Configuration,DC=domain,DC=com ';

$info['objectClass'][0]= 'top';
$info['objectClass'][1]= 'person';
$info['objectClass'][2]= 'organizationalPerson';
$info['objectClass'][3]= 'user'; 

$info['useraccountcontrol'] = 66048;

        $newpass = "112345";        
        $newPassword = "\"" . $newpass . "\"";        
        $len = strlen($newPassword);
        $newPassw='';
        for ($i = 0; $i < $len; $i++)
        $newPassw .= "{$newPassword{$i}}\000";        
        $newPassword = $newPassw; 
        $info["unicodepwd"] = $newPassword;    

$a=ldap_add($ds, $dn, $info);

if($a){
    echo "Entry Succesfull!";
}else{
    echo '</br>##-Entry Failed - '. ldap_err2str(ldap_errno($ds)) .' -##';
}
?>

但是我收到以下错误

 /*
    Warning: ldap_add(): Add: Server is unwilling to perform in /var/www/ADMS/adduser.php on line 88

    ##-Entry Failed - Server is unwilling to perform -##
    */

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

您可以更改此代码:

 $newpass = "112345";        
        $newPassword = "\"" . $newpass . "\"";        
        $len = strlen($newPassword);
        $newPassw='';
        for ($i = 0; $i < $len; $i++)
        $newPassw .= "{$newPassword{$i}}\000";        
        $newPassword = $newPassw; 
        $info["unicodepwd"] = $newPassword; 

有了这个:

$pwdtxt = "112345";
$newPassword = '"' . $pwdtxt . '"';
$newPass = iconv( 'UTF-8', 'UTF-16LE', $newPassword );
$info["unicodepwd"] = $newPassw;