通过php将用户添加到Active Directory

时间:2013-02-22 13:16:33

标签: active-directory

以下是我用于在Active Directory中添加用户的代码

<?php
$ldapConn = ldap_connect('ldap://xxx.xx.x.xx:389');
ldap_set_option($ldapConn, LDAP_OPT_PROTOCOL_VERSION, 3);

ldap_bind( $ldapConn, 'xx.xx@xx.xx', 'xxxx');

$dn_user='CN=testLDAP,OU=xx,DC=xx,DC=xx';

$ldaprecord['cn'] = "testLDAP";
$ldaprecord['givenName'] = "testLDAP";  
$ldaprecord['sn'] = "testLDAP";
$ldaprecord['sAMAccountName'] = "testLDAP";
$ldaprecord['UserPrincipalName'] = "testLDAP@xx.xx";
$ldaprecord['displayName'] = "testLDAP";
$ldaprecord['name'] = "testLDAP";
$ldaprecord['UserAccountControl'] = "544";
$ldaprecord['objectclass'][0] = 'top';
$ldaprecord['objectclass'][1] = 'person';
$ldaprecord['objectclass'][2] = 'organizationalPerson';
$ldaprecord['objectclass'][3] = 'user';
$ldaprecord['mail'] = "testLDAP@abc.com";

$add = ldap_add($ldapConn, $dn_user, $ldaprecord);

    if($add) {
        echo "User successfully added";
    } else {
        echo "User not added";
    }
ldap_close($ldapConn);
?>

但我收到的错误是

警告:ldap_add()[function.ldap-add]:添加:无法联系LDAP服务器

我的代码有什么问题,请帮帮我.. 提前谢谢..

1 个答案:

答案 0 :(得分:1)

您必须在SSL / TLS下连接才能创建用户或设置/更改密码,所有其他Active Directory功能都可以在纯文本连接下执行。

为了让您的生活更轻松,我建议您查看第三方图书馆:http://adldap.sourceforge.net/