user1606656
user1606656

Reputation: 63

Add user to Active Directory through php

Following is the code I am using to add user in Active Directory

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

ldap_bind( $ldapConn, '[email protected]', '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'] = "[email protected]";
$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'] = "[email protected]";

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

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

But I am getting the error saying

Warning: ldap_add() [function.ldap-add]: Add: Can't contact LDAP server

What's wrong with my code, please help me.. Thanks in advance..

Upvotes: 2

Views: 12480

Answers (1)

DragonZero
DragonZero

Reputation: 845

You must be connecting under SSL/TLS to be able to create users or set/change passwords, all other Active Directory functions can be executed under a plain text connection.

To make your life easier, I suggest you look into this 3rd party library : http://adldap.sourceforge.net/

Upvotes: 1

Related Questions