THIS IS AN OLD POST AND IS KEPT LIVE FOR ARCHIVING PURPOSES ONLY

Just a quickie for anyone looking to authorise users on a PHP driven website by directly connecting to a Windows Server’s Active Directory using LDAP. The little script below is a very (very) basic method to connect to an Active Directory (AD) and pull out all the details required.

If you are used to using MySQL to store user info and have used login systems which query the database and pull out the user’s details then you may find connecting to LDAP a little different. With a PHP LDAP connection you do not connect to the database and then see if a user exists, you actually connect to the AD using the user’s passed credentials and if it successfully connects then you know you are authenticated – you can then pull the users info out if required. and perform extra checks. By default the AD is readable by all authenticated users so you should not have permission problems unless the IT administrator has locked down the AD a little too much – which lets face it is highly possible!!

So – on to it. There ae two versions below – the first is for basic authentication and the second is for a connection over ldaps with TLS (or SSL) enabled. Both the below connect to to the AD server using LDAP/s, binds to AD using the user’s details to authenticate but the first returns all the users’ info using the query (cn=*) – the asterisk being a wildcard for all users – and the second one just pulls to required user’s info using the (cn=$ldapuser) query. Hopefully it all makes sense but if not then comment below and I’l try to do a bterr job of explaining it all.

One final thing I need to mention is that you need to make sure you query the correct AD tree – the first version below is set for a default Windows 2003 Small Business Server setup – SBSUsers/Users/MyBusiness/Domain/tld – you can change this by editing the $ldaptree variable to suit.

So – version one – return all users (default connection type):

http://mj7.co.uk/ajc3

And version 2:

http://mj7.co.uk/ajdw

So hopefully somene finds them useful – any questions just comment below.

As always – tweet if you like, link if you use.

6 Comments


  1. nattapon
    Oct 12, 2011

    thank a lot it very useful for me


  2. Lance V.
    Mar 29, 2012

    That is a really nice example version 2. The only catch I had was I changed (beside the original variables. cn=$ldapuser to sAMAccountName=$ldapuser. I also moved the filter part to a variable and then just used the variable for the filter.


  3. dequin_cl
    May 17, 2012

    Thank you very much 🙂 it really helped me get connected. Added sAMAccountName as Lance V. suggested though 😉


  4. Otobong
    Nov 10, 2012

    Hello the Links are not working please can you help restore it. Thanks


  5. mj7
    Nov 17, 2012

    Sorry about that – have moved servers and not checked certain links (schoolboy error). They work again now. Thanks for pointing it out.


  6. Jay
    May 15, 2013

    I used your version 1 script.

    Simple and easy. thanks.