Preventing PHP Warnings on LDAP Functions

ldap, php, suppress-warnings, warnings

Solution

This behaviour is by design, you cannot prevent ldap_bind from triggering a warning on invalid credentials. You still have some options, though:

- Suppress the warning with `@` as you are already doing

- Turn all errors into Exceptions, then catch them and handle appropriately

- Ignore warnings by modifying the error reporting level (very, very bad idea)

In my own ldap library I use the `@` suppressor, but I have heard that it is quite slow compared to converting an error into Exception, so my suggestion is to go with option 2. If you don't care about super-high performance, then option 1 is a perfectly valid approach.

Problem

Currently I am using the `ldap_*` functions to handle authentication for one of my web applications. I have logic that is able to check if the login is valid which works fine, however when a user enters an invalid username/password combination `ldap_bind()` produces a warning which I would like to avoid if possible. At the moment I am suppressing this error with the `@` operator but I am curious if there is a better way to block warnings from `ldap_*` without turning off warnings in PHP completely or suppressing them. The warning is A PHP Error was encountered Severity: Warning Message: ldap_bind() [function.ldap-bind]: Unable to bind to server: Invalid credentials Filename: libraries/userauth.php Line Number: 75 My current code is as follows: ``` $uid = "uid=".$username; $ldapUser = $uid.",ou=***,dc=***,dc=***"; $ds = ldap_connect( $this->ldapURL ); $lb = @ldap_bind( $ds, $ldapUser, $password ); $sr = ldap_search( $ds, $this->ldapBaseDN, $uid ); $info = ldap_get_entries( $ds, $sr ); ``` Is there any way to prevent this warning without turning of PHP warnings altogether or suppressing it?

Original source

Related problems