What are common pitfalls when using LDAP authentication in PHP?

Common pitfalls when using LDAP authentication in PHP include not properly escaping user input, not handling errors gracefully, and not securely storing LDAP connection information. To avoid these pitfalls, always sanitize and validate user input before using it in LDAP queries, implement error handling to catch and handle LDAP connection errors, and store LDAP connection information securely, such as in a configuration file outside of the web root.

// Example of properly escaping user input before using it in an LDAP query
$username = ldap_escape($_POST['username']);
$password = ldap_escape($_POST['password']);

// Example of implementing error handling for LDAP connection errors
$ldapconn = ldap_connect("ldap.example.com");
if (!$ldapconn) {
    die("Could not connect to LDAP server");
}

// Example of securely storing LDAP connection information
define('LDAP_SERVER', 'ldap.example.com');
define('LDAP_BIND_DN', 'cn=admin,dc=example,dc=com');
define('LDAP_BIND_PASSWORD', 'password123');