What are some best practices for troubleshooting LDAP connection failures in PHP?

When troubleshooting LDAP connection failures in PHP, ensure that the LDAP extension is installed and enabled in your PHP configuration. Check the LDAP server settings, such as the host, port, and bind credentials. Verify that the LDAP server is reachable from your PHP environment and that there are no network issues causing the connection failure.

// LDAP server settings
$ldap_host = 'ldap.example.com';
$ldap_port = 389;
$ldap_user = 'cn=admin,dc=example,dc=com';
$ldap_pass = 'password';

// Connect to LDAP server
$ldap_conn = ldap_connect($ldap_host, $ldap_port);

if (!$ldap_conn) {
    die("Failed to connect to LDAP server");
}

// Bind to LDAP server
$ldap_bind = ldap_bind($ldap_conn, $ldap_user, $ldap_pass);

if (!$ldap_bind) {
    die("Failed to bind to LDAP server");
}

echo "Successfully connected to LDAP server";