What is the purpose of LDAP authentication in PHP and how does it work?
LDAP authentication in PHP allows you to authenticate users against an LDAP directory server, which is commonly used in organizations for centralized user management. This allows you to verify user credentials and control access to your PHP application based on LDAP user accounts.
// LDAP server settings
$ldapServer = 'ldap.example.com';
$ldapPort = 389;
$ldapBaseDn = 'dc=example,dc=com';
// Connect to LDAP server
$ldapConn = ldap_connect($ldapServer, $ldapPort);
if (!$ldapConn) {
die("Could not connect to LDAP server");
}
// Bind to LDAP server with user credentials
$ldapBind = ldap_bind($ldapConn, 'cn=user,ou=users,'.$ldapBaseDn, 'password');
if (!$ldapBind) {
die("LDAP bind failed");
}
// LDAP authentication successful
echo "LDAP authentication successful";
// Close LDAP connection
ldap_close($ldapConn);