我有以下内容:
class LDAPConnection {
private $ldapServers = array(
"ldap://serv1", "ldap://serv2"
);
private $ldapUsername = "DOMAIN\\%s";
function login($username, $password) {
$user = sprintf($this->ldapUsername, $username);
// Make sure password is not empty (http://stackoverflow.com/a/172042/561731)
if(!empty($password)) {
foreach($this->ldapServers as $server) {
try {
$ldap = \ldap_connect($server);
\ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
if($bind = \ldap_bind($ldap, $user, $password)) {
// log them in
return true;
}
}
catch(\ErrorException $e) {
// do nothing
}
}
}
return false;
}
}
如您所见,我首先确保$password
不为空,然后尝试 ldap 连接,因为如果我不这样做,则 ldap 假定我要进行匿名连接并返回true
.
我该如何防止呢?我是否像上面那样做唯一的选择,我必须检查以确保密码不为空?或者,还有更好的方法?