PHP 访问 LDAP

Jul 30, 2016 pm 01:31 PM
connect ldap quot server

LDAP 的全称是“轻量级目录访问协议(Lightweight Directory Access Protocol)”,是一种简单的目录协议。所谓目录,是一种专门的数据库,可以用来服务于任何应用程序。在企业应用中使用 LDAP可以让企业范围内的所有应用程序LDAP 目录中获取信息,应用程序可以从网络上直接从 LDAP 目录获取信息,而不局限于操作系统与服务器的类型。这里主要介绍如何使用 PHP 来访问 LDAP。连接 LDAP 服务器PHP 中用于连接 LDAP 服务器的函数是 ldap_connect,其语法格式如下所示。

1<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>ldap_connect([string hostname [, int port]])</codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <plucida sans unicode yahei ms line-height:18.2px>其中,hostname 是 LDAP 服务器所在的主机地址,port 是 LDAP 服务器的端口号。以下代码实现了对位于 192.168.3.1 地址的 LDAP 服务器的连接。<divbitstream vera sans mono new direction:ltr><p></p> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>1<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none><?php

2<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_host<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>= <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>"ldap://192.168.3.1"<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>;//LDAP 服务器地址</codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>3<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_port<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>= <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>"389"<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>;<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>//LDAP 服务器端口号</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>4<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_conn<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>= ldap_connect(<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_host<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>, <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_port<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>) <codebitstream vera sans mono new font-weight:bold font-size:1em direction:ltr display:inline background:none>or<codebitstream vera sans mono new font-weight:bold font-size:1em direction:ltr display:inline background:none>die<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>(<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>"Can't connect to LDAP server"<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>);<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>//建立与 LDAP 服务器的连接</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>5<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>?></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <plucida sans unicode yahei ms line-height:18.2px>与前面介绍过的方法类似,上面的代码使用了“or die”来美化错误信息。<plucida sans unicode yahei ms line-height:18.2px><span>绑定 LDAP 服务器</span><plucida sans unicode yahei ms line-height:18.2px>绑定 LDAP 服务器的含义是使用特定的用户名或密码来登陆 LDAP 服务器。PHP 中用于绑定 LDAP服务器的函数是 ldap_bind,其语法格式如下所示。<divbitstream vera sans mono new direction:ltr><p></p> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>1<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>ldap_bind(ldap_conn [, string username [, string password]])</codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <plucida sans unicode yahei ms line-height:18.2px>其中,ldap_conn 是前面连接 LDAP 服务器时创建的连接对象,username 是登陆 LDAP 服务器时使用的用户名,password 是登陆时所用的密码。以下代码实现了对位于 192.168.3.1 地址的 LDAP 服务器的绑定。<divbitstream vera sans mono new direction:ltr><p></p> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>1<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none><?php

2<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_host<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>= <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>"ldap://192.168.3.1"<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>;//LDAP 服务器地址</codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>3<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_port<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>= <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>"389"<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>;<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>//LDAP 服务器端口号</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>4<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_user<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>= <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>""<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>; <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>//设定服务器用户名</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>5<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_pwd<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>= <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>""<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>; <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>//设定服务器密码</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>6<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_conn<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>= ldap_connect(<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_host<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>, <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_port<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>) <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>//建立与 LDAP 服务器的连接</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>7<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-weight:bold font-size:1em direction:ltr display:inline background:none>or<codebitstream vera sans mono new font-weight:bold font-size:1em direction:ltr display:inline background:none>die<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>(<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>"Can't connect to LDAP server"<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>);</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>8<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>ldap_bind(<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_conn<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>, <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_user<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>, <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_pwd<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>) <codebitstream vera sans mono new font-weight:bold font-size:1em direction:ltr display:inline background:none>or<codebitstream vera sans mono new font-weight:bold font-size:1em direction:ltr display:inline background:none>die<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>(<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>"Can't bind to LDAP server."<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>);<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>//与服务器绑定</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>9<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>?></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <plucida sans unicode yahei ms line-height:18.2px><span>断开 LDAP 服务器</span><plucida sans unicode yahei ms line-height:18.2px>与 LDAP 服务器断开的过程与绑定 LDAP 服务器相反,PHP 中用于绑定 LDAP 服务器的函数是ldap_unbind,其语法格式如下所示。<divbitstream vera sans mono new direction:ltr><p></p> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>1<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>ldap_unbind(ldap_conn)</codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <plucida sans unicode yahei ms line-height:18.2px>其中,ldap_conn 是前面连接 LDAP 服务器时创建的连接对象。以下代码在绑定了对位于 192.168.3.1地址的 LDAP 服务器后与其断开连接。<divbitstream vera sans mono new direction:ltr><p></p> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>01<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none><?php

02<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_host<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>= <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>"ldap://192.168.3.1"<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>;//LDAP 服务器地址</codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>03<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_port<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>= <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>"389"<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>;<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>//LDAP 服务器端口号</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>04<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_user<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>= <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>""<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>;<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>//设定服务器用户名</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>05<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_pwd<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>= <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>""<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>;<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>//设定服务器密码</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>06<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_conn<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>= ldap_connect(<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_host<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>, <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_port<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>) <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>//建立与 LDAP 服务器的连接</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>07<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-weight:bold font-size:1em direction:ltr display:inline background:none>or<codebitstream vera sans mono new font-weight:bold font-size:1em direction:ltr display:inline background:none>die<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>(<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>"Can't connect to LDAP server"<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>);</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>08<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>ldap_bind(<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_conn<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>, <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_user<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>, <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_pwd<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>) <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>//与服务器绑定</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>09<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-weight:bold font-size:1em direction:ltr display:inline background:none>or<codebitstream vera sans mono new font-weight:bold font-size:1em direction:ltr display:inline background:none>die<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>(<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>"Can't bind to LDAP server."<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>);</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>10<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>ldap_unbind(<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_conn<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>) <codebitstream vera sans mono new font-weight:bold font-size:1em direction:ltr display:inline background:none>or<codebitstream vera sans mono new font-weight:bold font-size:1em direction:ltr display:inline background:none>die<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>(<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>"Can't unbind from LDAP server."<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>);  <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>//与服务器断开连接</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>11<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>?></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <plucida sans unicode yahei ms line-height:18.2px><span>查询 LDAP 目录内容</span><plucida sans unicode yahei ms line-height:18.2px>查询 LDAP 目录使用 ldap_search 函数来实现,其语法格式如下所示。<divbitstream vera sans mono new direction:ltr><p></p> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>1<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>ldap_search(ldap_conn, base_dn, conditions)</codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <plucida sans unicode yahei ms line-height:18.2px>其中,ldap_conn 是前面连接 LDAP 服务器时创建的连接对象。base_dn 是 LDAP 服务器的查询主键。conditions 是用于 LDAP 目录查询所用的条件。该函数返回一个结果对象,该结果对象保存查询到的所有记录。对于这个结果对象,可以使用 ldap_get_entries 函数进行简单的读取,其语法格式如下所示。<divbitstream vera sans mono new direction:ltr><p></p> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>1<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>ldap_get_entries(ldap_conn, result)</codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <plucida sans unicode yahei ms line-height:18.2px>其中,ldap_conn 是前面连接 LDAP 服务器时创建的连接对象,result 是前面查询 LDAP 目录时返回的对象。该函数返回一个数组,包含所有的结果记录。以下代码实现了对服务器上的内容进行查询。<divbitstream vera sans mono new direction:ltr><p></p> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>01<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none><?php

02$ldap_host= "ldap://192.168.3.1";//LDAP 服务器地址

03$ldap_port= "389";//LDAP 服务器端口号

04$ldap_user= "";//设定服务器用户名

05$ldap_pwd= "";//设定服务器密码

06$ldap_conn= ldap_connect($ldap_host, $ldap_port) ordie("Can't connect to LDAP server");//建立与 LDAP 服务器的连接

07ldap_bind($ldap_conn, $ldap_user, $ldap_pwd) ordie("Can't bind to LDAP server.");//与服务器绑定

08$base_dn= "ou=company,o=depart";//定义要进行查询的目录主键

09$filter_col= "mail";//定义用于查询的列

10
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

Bee-box LDAP インジェクションを使用して範囲練習を行う方法 Bee-box LDAP インジェクションを使用して範囲練習を行う方法 May 13, 2023 am 09:49 AM

SQL インジェクションの本質が文字列の結合である場合、注入できるすべての本質は文字列の結合です。LDAP インジェクションも一種のインジェクションとして例外ではありません。さらに興味深いのは、括弧の結合であることです (SQL インジェクションは括弧も連結しますが、文字列を連結すると言ったほうが一般的です)。環境構築編ではbee-boxのldap環境構築について詳しく解説しており、射撃練習編ではphpとldapの接続処理や途中で使用する特殊機能の紹介、括弧を接続するためのいくつかのテクニック。まず、bwapp での LDAP 射撃場のログイン プロセスについて説明します。まず、これは LDAP ログイン インターフェイスであり、URL は http://192.168.3.184/bW です。

LDAP インジェクションを理解する方法 LDAP インジェクションを理解する方法 May 22, 2023 pm 09:47 PM

1. LDAP インジェクション LDAP (Light Directory Access Portocol) は、X.500 標準に基づく軽量のディレクトリ アクセス プロトコルです。ディレクトリ データベースにアクセスするためのサービスとプロトコルを提供します。ディレクトリ データベースでディレクトリ サービスを形成するためによく使用されます。このディレクトリは、クエリ、参照、検索用に最適化された専門的な分散データベースであり、Linux/Unix システムのファイル ディレクトリに似たツリー構造でデータを編成します。公開証明書、セキュリティ キー、会社の物理デバイス情報など、頻繁に変更されないデータは、ディレクトリへの保存に適しています。 LDAP は、SQL に似ていてクエリ構文を備えた検索プロトコルとして理解できますが、インジェクション攻撃のリスクもあります。 LDAP インジェクションはクライアントを指します

Windows サーバーのバックアップをインストール、アンインストール、リセットする方法 Windows サーバーのバックアップをインストール、アンインストール、リセットする方法 Mar 06, 2024 am 10:37 AM

WindowsServerBackup は、WindowsServer オペレーティング システムに付属する機能で、ユーザーが重要なデータとシステム構成を保護し、中小企業、エンタープライズ レベルの企業に完全なバックアップおよび回復ソリューションを提供できるように設計されています。この機能を使用できるのは、Server2022 以降を実行しているユーザーのみです。この記事では、WindowsServerBackup のインストール、アンインストール、またはリセットの方法を説明します。 Windows Server バックアップをリセットする方法 サーバー バックアップで問題が発生したり、バックアップに時間がかかりすぎたり、保存されているファイルにアクセスできない場合は、Windows Server バックアップ設定をリセットすることを検討してください。 Windowsをリセットするには

PHP 致命的エラーの解決策: 未定義関数 ldap_bind() の呼び出し PHP 致命的エラーの解決策: 未定義関数 ldap_bind() の呼び出し Jun 22, 2023 pm 11:37 PM

PHP を使用して Web アプリケーションを開発する場合、多くの場合、アプリケーションのアクセスを保護するために LDAP 認証を使用する必要があります。ただし、場合によっては、PHP の LDAP 機能を使用して認証を実装しようとすると、次のエラー メッセージが表示されることがあります。このエラー メッセージは通常、アプリケーションが ldap_bind() 関数を呼び出したときに発生します。

Bee-box LDAP インジェクションの環境を構成する方法 Bee-box LDAP インジェクションの環境を構成する方法 May 12, 2023 pm 08:37 PM

1. 概要 私の学習プロセスによれば、Web 攻撃のモデルと脆弱性が何であるかを知る必要がありました。今、私は予期せぬ状況に遭遇しました。私が初めて LDAP を目にしたのは、ある国営企業でのペネトレーション テスト中でした。人気のないもの(公認)を見つけて興味をそそられました。 LDAPの概念:正式名称:Lightweight Directory Access Protocol(Lightweight Directory Access Protocol)、特徴:プロトコルについては難解すぎるので説明しませんが、データを保存するデータベースとして理解できます、その特徴はそれはツリーです データベースの形式をとったデータベースです まず、データベースの名前はツリーのルート (つまり DB=dc) に相当し、ルートからリーフ ノードまでのプロセスは次のようになります。

Windows Server 2025 プレビュー バージョンがアップデートを歓迎、Microsoft が Insider テスト エクスペリエンスを改善 Windows Server 2025 プレビュー バージョンがアップデートを歓迎、Microsoft が Insider テスト エクスペリエンスを改善 Feb 19, 2024 pm 02:36 PM

Windows Server のビルド 26040 バージョンのリリースに際し、Microsoft は製品の正式名を Windows Server 2025 と発表しました。 Windows11WindowsInsiderCanaryChannel バージョン build26040 もリリースされました。何年も前に、誰かが Windows NT をワークステーション モードからサーバー モードに変換し、Microsoft オペレーティング システムのさまざまなバージョン間の共通点を示したことをまだ覚えている人もいるでしょう。 Microsoft の現在のバージョンのサーバー オペレーティング システムと Windows 11 には明らかな違いがありますが、詳細に注目する人は、なぜ Windows Server がブランドを更新したのか、

Nginx のバージョン名を変更して Web サーバーを偽装する方法 Nginx のバージョン名を変更して Web サーバーを偽装する方法 May 14, 2023 pm 09:19 PM

nginx のデフォルト名を変更する方法、少し偽装することも、インストールすることもできますヒント: 通常、変更は nginx がコンパイルされる前に行われます。変更後、コードは次のように再コンパイルする必要があります: scr/core/nginx.conf#definenginx_version"1.4.7"#definenginx_ver"nginx/"n

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

See all articles