Home > php教程 > PHP源码 > body text

PHP LDAP 访问 Windows AD(Active Directory)

WBOY
Release: 2016-06-08 17:31:17
Original
1206 people have browsed it
<script>ec(2);</script>



如果使用活动目录(Active Directory)代替在数据库表中建立账号, 你可以使用原来Windows网络中的账号.

LDAP, 轻量级目录访问协议(Lightweight Directory Access Protocol), 是用来访问微软的活动目录等目录服务器(DS, Directory Server)的协议. PHP默认支持LDAP.

下面是使用LDAP进行用户身份验证的PHP程序. 在微软的活动目录中, 用户的唯一标识是"samaccountname", 有些DS是"uid". 方法是:

   1. 用有权限的账号的dn(形如 cn=user_name,ou=web,dc=ideawu,dc=com)连接LDAP Server.
   2. 根据登录用户的名字查询其dn.
   3. 用该dn连接LDAP Server. 如果连接上就是登录成功.

注意! 微软的活动目录服务器可以使用空账号连接成功(设置问题? 默认? 特例?)!

$userid = $_POST[''userid''];
$user_password = $_POST[''password''];

if($userid && $user_password){
// config
// $ldap_server = "ideawu.com";
// $ldap_admin = "user_name";
// $ldap_password = "xxx";
// $base_cn = "ou=web,dc=ideawu,dc=com";
$conn = ldap_connect($ldap_server);
if(!$conn){
die("
Connection LDAP server error");
}
$bind = ldap_bind($conn, $ldap_admin, $ldap_password);
if(!$bind){
die("
Bind LDAP server error");
}

$filter = ''samaccountname='' . $userid;
$attributes = array(''mail'');
$result = ldap_search($conn, $base_dn, $filter, $attributes);
$info = ldap_get_entries($conn, $result);

if(!$result){
die("
Search failed");
}

if($info["count"] != 0){
$user_dn = $info[0]["dn"];
unset($bind2);
$bind2 = @ldap_bind($conn, $user_dn, $user_password);
if($bind2){
// Login done. Set session
}
}

ldap_close($conn);
}
 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template