Escapes a string for LDAP filters or distinguished-name values. This guide demonstrates its use in PHP code and explains when the result or side effect needs extra checks.
What It Does
Escapes a string for LDAP filters or distinguished-name values.
Syntax and Parameters
The examples below show a real call to ldap_escape with suitable arguments. Read the function call in context: its connection, handle, input data or other prerequisites are created before use.
Requirements: LDAP extension; an LDAPS directory and scoped credentials.
These examples are for an isolated development environment. Hostnames, credentials and remote paths must be replaced with your approved configuration.
Example 1: Core Behavior
Bind to a controlled directory and use a restricted service identity.
<?php
$filterValue = 'Ada* (QA)';
$resultValue = ldap_escape($filterValue, "", LDAP_ESCAPE_FILTER);
echo is_bool($resultValue) ? ($resultValue ? "true" : "false") : json_encode($resultValue, JSON_PARTIAL_OUTPUT_ON_ERROR);
Observe the returned value or changed resource state. A successful function call can still require additional checks before a higher-level operation is considered successful.
Example 2: Alternative or Failure Case
The second example uses a different directory record or input value.
<?php
$filterValue = 'Alice\\Admin';
$resultValue = ldap_escape($filterValue, "", LDAP_ESCAPE_FILTER);
echo is_bool($resultValue) ? ($resultValue ? "true" : "false") : json_encode($resultValue, JSON_PARTIAL_OUTPUT_ON_ERROR);
Compare the different inputs, environment assumptions or result checks. Do not use a transport-level success value as proof that all business-level requirements were met.
Practical Example: Applying the Rule
Check permissions, server controls and returned result types before modifying live directory data.
<?php
$filterValue = '[email protected]';
$resultValue = ldap_escape($filterValue, "", LDAP_ESCAPE_FILTER);
echo is_bool($resultValue) ? ($resultValue ? "true" : "false") : json_encode($resultValue, JSON_PARTIAL_OUTPUT_ON_ERROR);
In an application, convert low-level failures into controlled responses and log only non-sensitive diagnostic context.
Important Behavior and Edge Cases
Choose LDAP_ESCAPE_FILTER or LDAP_ESCAPE_DN correctly; they are not interchangeable.
LDAP filter values and DN values require different escaping flags. Never concatenate unchecked user input into a directory query.
Comparison with Related Functions or Patterns
| Approach | When it differs |
|---|---|
| ldap_escape | Escapes a string for LDAP filters or distinguished-name values. |
| Related API or pattern | htmlspecialchars escapes HTML output, not LDAP filter syntax. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Building an LDAP filter by concatenating unchecked user input | Choose LDAP_ESCAPE_FILTER or LDAP_ESCAPE_DN correctly; they are not interchangeable. |
| Substituting a related API without checking its semantics | htmlspecialchars escapes HTML output, not LDAP filter syntax. |
Key Takeaways
- Escapes a string for LDAP filters or distinguished-name values.
- Choose LDAP_ESCAPE_FILTER or LDAP_ESCAPE_DN correctly; they are not interchangeable.
- htmlspecialchars escapes HTML output, not LDAP filter syntax.
Select this API or pattern based on its documented behavior and the trust boundaries in your own application, and test all external integrations against their actual deployments.