HOWTO bind a ldap server by php?

如何使用 php 連結 ldap server?

下面是我的程式範例 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
$ldap_host = "ldaps://ldap.liho.tw";
$ldap_port = 636;
$admin = "cn=Manager, dc=liho,dc=tw";
$passwd = "XxXxXxXxXxXxXxXxXxXxXxXxXxXxXx";
 
ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);
 
$ds = ldap_connect($ldap_host, $ldap_port);
 
if (!$ds) {
    echo "ldap_connect() failed\n";
    exit;
}
 
if (ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3)) {
   echo "Using LDAPv3\n";
} else {
   echo "Failed to set protocol version to 3\n";
}
// ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
 
// if (!@ldap_start_tls($ds)) echo "start tls failed\n";
 
if ($ds) {
    $r = ldap_bind($ds, $admin, $passwd);
    if(!$r) die("ldap_bind failed<br>");
   echo "ldap_bind success";
    ldap_close($ds);
} else {
    echo "Unable to connect to LDAP server";
}