Configuration and connecting

Configuration and connecting – How to configure Net_LDAP2 and connect to an LDAP server

Connecting to an LDAP server

To connect to an LDAP server, you should use Net_LDAP2's static connect() method. It takes one parameter, an array full of configuration options, and either returns a Net_LDAP2 object if connecting works, or a Net_LDAP2_Error object in case of a failure.

The following table lists all configuration options. If the default value for an option fits your needs, you don't need add it to your configuration array.

Possible configuration options
Name Description Default
host LDAP server name to connect to. You can provide several hosts in an array in which case the hosts are tried from left to right. localhost
port Port on the server 389
version LDAP version 3
starttls TLS is started after connecting false
binddn The distinguished name to bind as (username). If you don't supply this, an anonymous bind will be established. (none)
bindpw Password for the binddn. If the credentials are wrong, the bind will fail server-side and an anonymous bind will be established instead. An empty bindpw string requests an unauthenticated bind. This can cause security problems in your application, if you rely on a bind to make security decisions (see RFC-4513, section 6.3.1). (none)
basedn LDAP base name (root directory) (none)
options Array of additional ldap options as key-value pairs array()
filter Default search filter (string or preferably Net_LDAP2_Filter object). See LDAP filters (objectClass=*)
scope Default search scope, see Search sub

Connecting to an LDAP server

<?php
// Inclusion of the Net_LDAP2 package:
require_once 'Net/LDAP2.php';

// The configuration array:
$config = array (
    
'binddn'    => 'cn=admin,ou=users,dc=example,dc=org',
    
'bindpw'    => 'password',
    
'basedn'    => 'dc=example,dc=org',
    
'host'      => 'ldap.example.org'
);

// Connecting using the configuration:
$ldap Net_LDAP2::connect($config);

// Testing for connection error
if (PEAR::isError($ldap)) {
    die(
'Could not connect to LDAP-server: '.$ldap->getMessage());
}
?>
How handling errors works in Net_LDAP2 (Previous) Searching entries (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

Note by: danburkert@gmail.com
The code snippet here, as well as the one in the docs included with the package say include-once "Net/ldap.php". At least for my installation this was incorrect, the correct file is "Net/ldap.php". It took me (first time working with pear, fairly inexperienced programmer) about 30 mins to track this down, the docs should be fixed.
Note by: steve@computurn.com
As per hansfn's note in ldap_set_option, if your connection is failing, you can turn on debugging by:

ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);
Note by: sean
If your using the Net_LDAP2 package then
require_once 'Net/LDAP.php'; should be
require_once 'Net/LDAP2.php';