What are some common troubleshooting steps when encountering issues with LDAP authentication in PHP scripts?

When encountering issues with LDAP authentication in PHP scripts, common troubleshooting steps include checking the LDAP server connection details, verifying the LDAP server is reachable from the PHP server, ensuring the PHP LDAP extension is installed and enabled, and double-checking the LDAP bind credentials.

// LDAP server connection details
$ldap_server = 'ldap://ldap.example.com';
$ldap_port = 389;

// Verify LDAP server is reachable
if (!$ldap_conn = ldap_connect($ldap_server, $ldap_port)) {
    die('Could not connect to LDAP server');
}

// Ensure PHP LDAP extension is installed and enabled
if (!extension_loaded('ldap')) {
    die('LDAP extension not installed or enabled');
}

// Double-check LDAP bind credentials
$ldap_bind_dn = 'cn=admin,dc=example,dc=com';
$ldap_bind_pass = 'password';

if (!ldap_bind($ldap_conn, $ldap_bind_dn, $ldap_bind_pass)) {
    die('LDAP bind failed');
}

// LDAP authentication successful, continue with your code here