PHPLDAP accessing WindowsAD (ActiveDirectory)-PHP source code

Source: Internet
Author: User
Tags samaccountname
Ec (2); if you use the Active Directory instead of creating an account in the database table, you can use the account in the original Windows network. LDAP, Lightweight Directory Access Protocol (LightweightDirectoryAccessProtocol), is used to access Microsoft's Active Directory and other directory servers (DS, DirectoryServer) protocol. PHP supports LDAP by default. the following uses L script ec (2); script




If you use the Active Directory instead of creating an account in the database table, you can use the account in the original Windows network.

Lightweight Directory Access Protocol (Lightweight Directory Access Protocol) is a Protocol used to Access Microsoft's Active Directory and other Directory servers (DS, Directory Server). PHP supports LDAP by default.

The following is a PHP program that uses LDAP for user authentication. in Microsoft's Active Directory, the Unique User ID is "samaccountname", and some DS are "uid". The method is as follows:

1. Connect to the LDAP Server with the dn (such as cn = user_name, ou = web, dc = ideawu, dc = com) of the account with permissions.
2. query the dn of a logon user based on the user name.
3. Use this dn to connect to the LDAP Server. If the connection is successful, the login is successful.

Note! Microsoft's Active Directory Server can be connected successfully with an empty account (set question? Default? Exception ?)!

$ Userid = $ _ POST [''userid''];
$ User_password = $ _ POST [''password''];

If ($ userid & $ user_password ){
// Config
// $ Ldap_server = "ideawu.com ";
// $ Ldap_admin = "user_name ";
// $ Ldap_password = "xxx ";
// $ Base_cn = "ou = web, dc = ideawu, dc = com ";
$ Conn = ldap_connect ($ ldap_server );
If (! $ Conn ){
Die ("
Connection LDAP server error ");
}
$ Bind = ldap_bind ($ conn, $ ldap_admin, $ ldap_password );
If (! $ Bind ){
Die ("
Bind LDAP server error ");
}

$ Filter = ''samaccountname = ''. $ userid;
$ Attributes = array (''mail '');
$ Result = ldap_search ($ conn, $ base_dn, $ filter, $ attributes );
$ Info = ldap_get_entries ($ conn, $ result );

If (! $ Result ){
Die ("
Search failed ");
}

If ($ info ["count"]! = 0 ){
$ User_dn = $ info [0] ["dn"];
Unset ($ bind2 );
$ Bind2 = @ ldap_bind ($ conn, $ user_dn, $ user_password );
If ($ bind2 ){
// Login done. Set session
}
}

Ldap_close ($ conn );
}


</

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.