我知道之前已经有过这样的答案,但是它无法帮助我(除非它有,但由于我有限的PHP知识,它没有帮助)。以下是我的代码:
<body>
<html>
<?php
//echo var_dump($_POST);
$user = "".$_POST["username"]."";
settype($user, "string");
$password = $_POST["password"];
$ldap_host = "ldap.burnside.school.nz";
$base_dn = "ou=students,o=bhs";
$ldap_user = "(cn=".$user.")";
$filter = "($ldap_user)"; // Just results for this user
$ldap_pass = "".$password."";
$connect = ldap_connect($ldap_host)
or exit(">>Could not connect to LDAP server<<");
ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
// This next bit is the important step. Bind, or fail to bind. This tests the username/password.
if (ldap_bind($connect, $ldap_user.",".$base_dn, $ldap_pass)) {
$read = ldap_search($connect, $base_dn, $filter)
or exit(">>Unable to search ldap server<<");
// All the next 8 lines do is get the users first name. Not required
$info = ldap_get_entries($connect, $read);
$ii = 0;
for ($i = 0; $ii < $info[$i]["count"]; $ii++) {
$data = $info[$i][$ii];
if ($data == "givenname") {
$name = $info[$i][$data][0];
}
}
ldap_close($connect);
header("Location: success.php?name=$name");
}
else {
ldap_close($connect);
//header("Location: failure.php?user=$user");
}
?>
</body>
</html>
我在第21行收到错误,就是当我绑定到服务器时说:
警告:ldap_bind():无法绑定到服务器:第21行的S:\ XAMPP \ htdocs \ PhpProject1 \ LDAP_main.php中的DN语法无效
有人能解决这个问题吗?当我在代码中实现我的$_POST
以接收用户名和密码时,它才刚刚开始发生,但正如您在注释// echo var_dump($_POST)
时所看到的那样,我实际上正在接收我想要的数据。
答案 0 :(得分:3)
绑定到LDAP服务器的DN是(cn=[username]),ou=students,o=bhs
,它不是有效的DN语法。这应该是cn=[username],ou=students,o=bhs
没有大括号。
您已将LDAP过滤器(大括号内的内容)与DN混合在一起。
我将通过以下方式进行LDAP身份验证:
(|(mail=[username])(cn=[username])(uid=[username]))
之类的过滤器来查找在mail,cn或uid-attribute中具有用户名的条目