如果使用活动目录(Active Directory)代替在数据库表中建立账号, 你可以使用原来Windows网络中的账号.
LDAP, 轻量级目录访问协议(Lightweight Directory Access Protocol), 是用来访问微软的活动目录等目录服务器(DS, Directory Server)的协议. PHP默认支持LDAP.
下面是使用LDAP进行用户身份验证的PHP程序. 在微软的活动目录中, 用户的唯一标识是"samaccountname", 有些DS是"uid". 方法是:
- 用有权限的账号的dn(形如 cn=user_name,ou=web,dc=ideawu,dc=com)连接LDAP Server.
- 根据登录用户的名字查询其dn.
- 用该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("<br>Connection LDAP server error"); } $bind = ldap_bind($conn, $ldap_admin, $ldap_password); if(!$bind){ die("<br>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("<br>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); }