Authenticate Active Directory user through LDAP in PHP:
– Create login form with two field username and password. – On submit of this login form get the submitted data…
– Append the ‘Account Suffix’ with the username. – Connect AD server to your local setup using ‘ldap_connect’ function.
– Check whether the provided details are valid or not using ‘ldap_bind’ function.
|
Example code: |
<?php // LDAP variables $ldaphost = "ldap.example.com"; // your ldap servers $ldapport = 389 or 636; // your ldap server's port number // Connecting to LDAP $ldapconn = ldap_connect($ldaphost, $ldapport) or die("Failed to connect"); //username should have 'Account Suffix' as suffix //username= 'user@AccountSuffix' $ldapuname = $_POST['username'] . '@AccountSuffix'; // associated password posted via login form $ldappass = $_POST['password']; // verify binding if ($ldapbind) { echo "LDAP bind successful... User authenticated successfully"; } else { echo "LDAP bind failed..."; } ?>