Maison développement back-end tutoriel php 用Php如何操作LDAP_PHP

用Php如何操作LDAP_PHP

Jun 01, 2016 pm 12:43 PM
ds info ldap 如何 操作 utiliser 连接

LDAP

1 LDAP是什么
        LDAP是一个用来发布目录信息到许多不同资源的协议。通常它都作为一个集中的地址本使用,不过根据组织者的需要,它可以做得更加强大。
  LDAP最基本的形式是一个连接数据库的标准方式。该数据库为读查询作了优化。因此它可以很快地得到查询结果,不过在其它方面,例如更新,就慢得多。要特别注意的是,LDAP通常作为一个hierarchal数据库使用,而不是一个关系数据库。因此,它的结构用树来表示比用表格好。正因为这样,就不能用SQL语句了。

  简单说来,LDAP是一个得到关于人或者资源的集中、静态数据的快速方式。

    LDAP是轻量目录访问协议(Lightweight Directory Access Protocol)的缩写,其实是一话号码簿,类似于我们所使用诸如NIS(Network Information Service)、DNS (Domain Name Service)等网络目录,也类似于你在花园中所看到的树木。 
    LDAP是一种特殊的数据库。但是LDAP和一般的数据库不同,明白这一点是很重要的。 LDAP对查询进行了优化,与写性能相比LDAP的读性能要优秀很多。
1.1 LDAP的存储规则
    区分名(DN,Distinguished Name) 
    和自然界中的树不同,文件系统/LDAP/电话号码簿目录的每一片枝叶都至少有一个独一无二的属性,这一属性可以帮助我们来区别这些枝叶。 
    在文件系统中, 这些独一无二的属性就是带有完整路径的文件名。比如/etc/passwd,该文件名在该路径下是独一无二的。当然我们可以有/usr/passwd, /opt/passwd,但是根据它们的完整路径,它们仍然是唯一的。 
    在LDAP中,一个条目的区分名称叫做“dn”或者叫做区分名。在一个目录中这个名称总是唯一的。比如,我的dn是"uid=aghaffar, ou=People, o=developer.ch"。不可能有相同的dn,但是我们可以有诸如"uid=aghaffar, ou=Administrators, o=developer.ch"的dn。这同上面文件系统中/etc/passwd 和 /usr/passwd的例子很类似。 
    我们有独一无二的属性,在"ou=Administrators, o=developer.ch" 中uid和在"ou=People, o=developer.ch"中的uid。这并不矛盾。
CN=Common Name 为用户名或服务器名,最长可以到80个字符,可以为中文;
OU=Organization Unit为组织单元,最多可以有四级,每级最长32个字符,可以为中文;
O=Organization 为组织名,可以3—64个字符长
C=Country为国家名,可选,为2个字符长

    LDAP目录以一系列“属性对”的形式来存储记录项,每一个记录项包括属性类型和属性值(这与关系型数据库用行和列来存取数据有根本的不同)。
mail = testmail@mccc.net
othermailbox = testmailother@mccc.com
givenname = givenname
sn = test sn
属性可添加,以下一个属性必须赋值:
objectclass=person (值为:person 或 server 或 organization 或 其他自定义的值)

2 Php如何操作LDAP
2.1 Php如何与LDAP连接和关闭
$ds=ldap_connect("ServerName")
ServerName是LDAP的服务器名,

例:
$ds=ldap_connect(“10.31.172.30:1000”)
返回值是:true 或 false

关闭连接
ldap_close($ds);

2.2 在php中如何搜索用户信息

$ds=ldap_connect("10.31.172.30:1000");
//首先连接上服务器
$justthese = array("cn","userpassword",”location”);
//搜索函数中的一个参数,要求返回哪些信息,
//以上传回cn,userpassword,location,这些都要求小写
$sr=ldap_search($ds,"o=jite", "cn=dom*",$justthese); 
//第一个参数开启LDAP的代号
//第二个参数最基本的 dn 条件值 , 例:”o=jite,c=cn”
//第三个参数 filter 为布林条件,它的语法可以在 Netscape 站上找一份 dirsdkpg.pdf 档案.
// ’o’为组织名,’cn’ 为用户名,用户名可用通配符 ’*’
echo "domadmin姓氏有".ldap_count_entries($ds,$sr)." 个

";
//ldap_count_entries($ds,$sr)传回记录总数

$info = ldap_get_entries($ds, $sr);
//LDAP的全部传回资料
echo "资料传回 ".$info["count"]."笔:

";
for ($i=0; $iecho "dn为:". $info[$i]["dn"] ."
";
echo "cn为:". $info[$i]["cn"][0] ."
"; //显示用户名
echo "email为:". $info[$i]["mail"][0] ."

"; //显示mail
echo "email为:". $info[$i][“userpassword"][0] ."

"; //显示加密后的密码
}
2.3 添加用户
$ds=ldap_connect("10.31.172.30:1000");
//首先连接上服务器
$r=ldap_bind($ds,"cn=domadmin,o=jite","password");
//系住一个管理员,有写的权限
// cn=domadmin,o=jite顺序不能变
$info["cn"]="aaa"; //必填 
$info["userpassword"]="aaa"; 
$info["location"]="shanghai";
$info["objectclass"] = "person"; //必填person为个人,还有server…
ldap_add($ds, "cn=".$info["cn"].",o=jite", $info);
ldap_unbind($ds);
//取消绑定
ldap_close($ds);
//关闭连接
2.4 删除用户
$ds=ldap_connect("10.31.172.30:1000");
//首先连接上服务器 
ldap_bind($ds,"cn=domadmin,o=jite","password");
//绑定管理员,有删除的权限
$dn="cn=dingxf,o=jite";
ldap_delete($ds, $dn);
//删除用户
ldap_unbind($ds);
//取消绑定
ldap_close($ds);
//关闭连接
2.5 修改用户资料
$ds=ldap_connect("10.31.172.30:1000");
//首先连接上服务器 
ldap_bind($ds,"cn=domadmin,o=jite","password");
//绑定管理员,有修改的权限
$dn="cn=dingxf,o=jite"; 
//用户dn
$info["userpassword"]="aaa"; //要修改的信息,放在数组变量中
$info["location"]="shanghaisdaf";

ldap_modify($ds, $dn , $info);
//修改函数
ldap_unbind($ds);
//取消绑定
ldap_close($ds);
//关闭连接
2.6 用户登录验证
$ds=ldap_connect("10.31.172.30:1000");
//首先连接上服务器 
if (ldap_bind($ds,"cn=dingxf,o=jite","dingxf")){
echo "验证通过";
}else{
echo "验证不通过";
}
ldap_unbind($ds);
//取消绑定
ldap_close($ds);
//关闭连接

   


注:此方法比较简单,实用,它也有不足之处,如果不通过,ldap_bind()提示它自带的提示语:”Warning: LDAP: Unable to bind to server: Inappropriate authentication in /home/htdocs/jldl.net/ldap/test.php3 on line 16”
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Outils d'IA chauds

Undresser.AI Undress

Undresser.AI Undress

Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover

AI Clothes Remover

Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool

Undress AI Tool

Images de déshabillage gratuites

Clothoff.io

Clothoff.io

Dissolvant de vêtements AI

AI Hentai Generator

AI Hentai Generator

Générez AI Hentai gratuitement.

Article chaud

R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
2 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Repo: Comment relancer ses coéquipiers
4 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD

Outils chauds

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Comment connecter la balance de graisse corporelle Comment connecter la balance de graisse corporelle Mar 07, 2024 pm 04:50 PM

Comment connecter la balance de graisse corporelle

Tutoriel d'utilisation de PyCharm : vous guide en détail pour exécuter l'opération Tutoriel d'utilisation de PyCharm : vous guide en détail pour exécuter l'opération Feb 26, 2024 pm 05:51 PM

Tutoriel d'utilisation de PyCharm : vous guide en détail pour exécuter l'opération

Qu'est-ce que sudo et pourquoi est-ce important ? Qu'est-ce que sudo et pourquoi est-ce important ? Feb 21, 2024 pm 07:01 PM

Qu'est-ce que sudo et pourquoi est-ce important ?

L'imprimante partagée ne peut pas se connecter à l'imprimante L'imprimante partagée ne peut pas se connecter à l'imprimante Feb 22, 2024 pm 01:09 PM

L'imprimante partagée ne peut pas se connecter à l'imprimante

Comment connecter la montre OnePlus au casque Bluetooth_Comment connecter la montre OnePlus au casque Bluetooth Comment connecter la montre OnePlus au casque Bluetooth_Comment connecter la montre OnePlus au casque Bluetooth Mar 23, 2024 pm 01:16 PM

Comment connecter la montre OnePlus au casque Bluetooth_Comment connecter la montre OnePlus au casque Bluetooth

Étapes et précautions de fonctionnement de Linux Deploy Étapes et précautions de fonctionnement de Linux Deploy Mar 14, 2024 pm 03:03 PM

Étapes et précautions de fonctionnement de Linux Deploy

Pourquoi n'y a-t-il aucun son une fois mon téléphone connecté à un casque Bluetooth ? Pourquoi n'y a-t-il aucun son une fois mon téléphone connecté à un casque Bluetooth ? Feb 20, 2024 am 11:42 AM

Pourquoi n'y a-t-il aucun son une fois mon téléphone connecté à un casque Bluetooth ?

Étapes pour connecter le contrôleur de jeu à Gohan Arcade Étapes pour connecter le contrôleur de jeu à Gohan Arcade Mar 19, 2024 pm 03:55 PM

Étapes pour connecter le contrôleur de jeu à Gohan Arcade

See all articles