Php connection to LDAP Server (ActiveDirectory) and Information Retrieval

Source: Internet
Author: User
Tags openldap
LDAP is a Protocol, which is commonly used in the following three solutions: 1NDS (NovellDirectoryServices) 2MicrosoftActiveDirectory3OpenLDAP to implement LDAP, the first step is to design DIT (DirectoryInformationTree ). The following connection code is verified under MicrosoftActiveDirectory (AD)

LDAP is a Protocol. Currently, there are three common implementations: 1 NDS (Novell Directory Services) 2 Microsoft Active Directory 3 OpenLDAP to implement LDAP, the first step is to design the DIT (Directory Information Tree ). The following connection code is verified under Microsoft Active Directory (AD)

  LDAP is a Protocol. Currently, there are three common implementations:

1 NDS (Novell Directory Services)

2 Microsoft Active Directory

3 OpenLDAP

To implement LDAP, the first step is to design the DIT (Directory Information Tree ).

BelowConnectionThe code is verified under Microsoft Active Directory (AD.

  The simplest bind method:


// LDAP variables
$ Ldaphost = "192.168.8.5"; // your ldap servers
$ Ldapport = 389; // your ldap server's port number

// Connecting to LDAP
$ Ldapconn = ldap_connect ($ ldaphost, $ ldapport) or die ("cocould not connect to $ ldaphost ");


$ Ldaprdn = "testuser"; // ldap rdn or dn
$ Ldappass = 'testpwd'; // associated password

If ($ ldapconn ){

// Binding to ldap server
$ Ldapbind = ldap_bind ($ ldapconn, $ ldaprdn, $ ldappass );

// Var_dump ($ ldapbind );

// Verify binding
If ($ ldapbind ){
Echo "LDAP bind successful ...";
} Else {
Echo "LDAP bind failed ...";
}

}

Whether this method can be used depends on the structure of the Directory Information Tree. This method has obvious disadvantages: either bind succeeded or failed. That is, it cannot be a user name error or a Password error.

  Search Method:

$ Ldap_host = "192.168.8.5 ";

$ Ldap_port = "389 ";

$ Base_dn = "OU = zzz, DC = test, DC = com, DC = cn ";

$ Filter = "(cn = *)";

$ Ldap_user = "cn = admin, OU = zzz, DC = test, DC = com, DC = cn ";

$ Ldap_pass = "123456 ";

$ Connect = ldap_connect ($ ldap_host, $ ldap_port );

Ldap_set_option ($ connect, LDAP_OPT_PROTOCOL_VERSION, 3 );

$ Bind = ldap_bind ($ connect, $ ldap_user, $ ldap_pass );

$ Read = ldap_search ($ connect, $ base_dn, $ filter );

$ Info = ldap_get_entries ($ connect, $ read );

Echo $ info ["count"]. "entrees retournees

";

For ($ ligne = 0; $ ligne <$ info ["count"]; $ ligne ++)

{

For ($ colonne = 0; $ colonne <$ info [$ ligne] ["count"]; $ colonne ++)

{

$ Data = $ info [$ ligne] [$ colonne];

Echo $ data. ":". $ info [$ ligne] [$ data] [0]."
";

}

Echo"
";

}

Ldap_close ($ connect );

Where:

$ Ldap_user = "cn = admin, OU = zzz, DC = test, DC = com, DC = cn ";

$ Ldap_pass = "123456 ";

These two statements define a dedicated LDAP account used to log on to LDAPServer, This account must haveSearchPermission.

Log on to LDAPServerThen, you canInformationProceedSearch(Ldap_search), you can determine whether an id exists, whether the password is correct, and so on.

The method used in specific development depends on the design of the Directory Information Tree.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.