如何在此ldap_connect代码中包含域名(php)

时间:2013-06-07 18:17:16

标签: php ldap

// User Custom Validate event
function User_CustomValidate(&$usr, &$pwd) {
// Enter your custom code to validate user, return TRUE if valid.     


// LDAP authentication example for User_CustomValidate server event
if (!function_exists("ldap_connect"))
    die("LDAP extension not installed.");
$ldapconn = ldap_connect("server.company.com", 389) or die("Could not connect to LDAP server."); // Note: Replace the host name and port
if ($ldapconn && ldap_bind($ldapconn, $usr, $pwd)) {
    $this->setCurrentUserName($usr); // Set the current user name    
    return TRUE;
}

return FALSE;

}

我有一个应用程序使用此代码块来促进LDAP身份验证。在登录页面上,用户必须放入company \ user.name - 如何在此代码中将“company”部分连接到usr变量?

1 个答案:

答案 0 :(得分:0)

您只需在用户名中包含“company”部分即可。您可以在公司域中包含调用验证:User_CustomValidate('username', 'password', 'company');,也可以在函数中设置$domain的默认值,以便自行处理:function User_CustomValidate(&$usr, &$pwd, $domain = 'company') {

这应该可以解决问题:

// User Custom Validate event
function User_CustomValidate(&$usr, &$pwd, $domain = '') {
    // Enter your custom code to validate user, return TRUE if valid.     

    //Add the slashes to domain if necessary
    $domain = (!empty($domain))?$domain.'\\':'';

    // LDAP authentication example for User_CustomValidate server event
    if (!function_exists("ldap_connect"))
        die("LDAP extension not installed.");
    $ldapconn = ldap_connect("server.company.com", 389) or die("Could not connect to LDAP server."); // Note: Replace the host name and port
    if ($ldapconn && ldap_bind($ldapconn, $domain.$usr, $pwd)) {
        $this->setCurrentUserName($usr); // Set the current user name    
        return TRUE;
    }

    return FALSE;
}