早上好,
我有一个提供 LDAP 身份验证(下面的代码)的 php 脚本,但是我需要对多个上下文进行身份验证。
我不确定解决此问题的最佳方法,并想知道是否有人可能有任何指示/建议。
亲切的问候,本
$username = $_POST["username"];
$password = $_POST["password"];
//$ipaddressreceived = $_POST["ipaddress"];
$ldapcontext = "ou=Staff, ou=Users,o=ABC";
$ldapconn = ldap_connect("IPADDRESS"); // must be a valid LDAP server!
$ldaprdn = "cn=$username,$ldapcontext";
function authenticate($username, $password) {
global $ldapconn;
global $username;
global $password;
global $ldaprdn;
// Prevent null binding
if ($username === NULL || $password === NULL) { return false; }
if (empty($username) || empty($password)) { return false; }
// Bind as the user
$ldapbind = ldap_bind($ldapconn, $ldaprdn, $password);;
if ($ldapbind) { $ret = true;
} else {
$ret = false;
}
return $ret;
}
$authUser=authenticate($username, $password);
if ($authUser == true) {
session_start();
$_SESSION["username"]=$username;
$ldapfields = array("cn", "givenname", "sn", "mail");
$ldapsearch=ldap_search($ldapconn, "$ldapcontext", "cn=$username", "$ldapfields");
$info = ldap_get_entries($ldapconn, $ldapsearch);
$givenname = $info["sn"][0];
$_SESSION["givenname"]=$givenname;
$login_message = "LOGIN SUCCESSFUL";
header("Location: index.php");
exit;
} else
{
$login_message = "Login Failed";
}