|
<?php
// AD服务器的地址和端口
$adServer = "ldap://your-ad-server.com";
$ldapPort = 389; // 默认LDAP端口
// 绑定到AD的用户名和密码
$ldapUser = "CN=Your User,OU=Users,DC=yourdomain,DC=com";
$ldapPassword = "yourpassword";
// 初始化LDAP连接
$ldapConn = ldap_connect($adServer, $ldapPort);
if ($ldapConn) {
// 设置LDAP选项
ldap_set_option($ldapConn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapConn, LDAP_OPT_REFERRALS, 0);
// 绑定到AD服务器
if (ldap_bind($ldapConn, $ldapUser, $ldapPassword)) {
echo "成功绑定到AD服务器\n";
// 定义搜索基础DN和过滤条件
$baseDn = "DC=yourdomain,DC=com";
$filter = "(objectClass=user)";
// 执行搜索
$searchResult = ldap_search($ldapConn, $baseDn, $filter);
if ($searchResult) {
$entries = ldap_get_entries($ldapConn, $searchResult);
echo "找到 " . $entries["count"] . " 个条目\n";
// 遍历结果并输出用户信息
for ($i = 0; $i < $entries["count"]; $i++) {
echo "DN: " . $entries[$i]["dn"] . "\n";
echo "姓名: " . $entries[$i]["cn"][0] . "\n";
echo "邮箱: " . $entries[$i]["mail"][0] . "\n";
echo "---------------------------\n";
}
} else {
echo "搜索失败: " . ldap_error($ldapConn) . "\n";
}
// 解除绑定
ldap_unbind($ldapConn);
} else {
echo "绑定失败: " . ldap_error($ldapConn) . "\n";
}
} else {
echo "无法连接到AD服务器\n";
}
?>
|
|