php操作ldap

发布时间:2025-12-09 12:05:10 浏览次数:1

前言

对接ad域,采用的是ldap协议

代码展示

连接ldap

public function connect(){self::$connect = ldap_connect($this->account,$this->port);ldap_set_option(self::$connect,LDAP_OPT_PROTOCOL_VERSION,3);ldap_set_option(self::$connect, LDAP_OPT_REFERRALS, 0);$bind = ldap_bind(self::$connect,'用户名','密码');if ($bind){return true;}else{return false;}}

ldap过滤搜索

public function ldapFilter($baseDn,$filter){$read = ldap_search(self::$connect,$baseDn,$filter);var_dump(ldap_error(self::$connect));//输出错误日志//该结果需要手动处理下(返回自己有用的信息)$data = ldap_get_entries(self::$connect, $read); //获取结果数组if (!$data){return [];}return $data;}

处理windows限制1000条,优化获取数据

//处理windows限制1000条数据(进行分段读取)public function ldapFilter($baseDn,$filter){$data = [];$cookie = '';do {$result = ldap_search(self::$connect, $baseDn, $filter, [], 0, 0, 0, LDAP_DEREF_NEVER,[['oid' => LDAP_CONTROL_PAGEDRESULTS, 'value' => ['size' => 750, 'cookie' => $cookie]]]);ldap_parse_result(self::$connect, $result, $errcode , $matcheddn , $errmsg , $referrals, $controls);// To keep the example short errors are not tested$entries = ldap_get_entries(self::$connect, $result);//array_shift($entries);$data = array_merge($data, $entries);// var_dump(count($data) . $filter);if (isset($controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'])) {// You need to pass the cookie from the last call to the next one$cookie = $controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'];} else {$cookie = '';}// Empty cookie means last page} while (!empty($cookie));if (!$data){return [];}return $data;}

参考链接:
PHP: ldap_search - Manual

PHPLDAP
需要做网站?需要网络推广?欢迎咨询客户经理 13272073477