简体   繁体   中英

How to include domain name in this ldap_connect code (php)

// 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;

}

I have an application that uses this block of code to facilitate LDAP authentication. On the login page, users must put in company\\user.name - How can I concatenate the "company\\" part to the usr variable in this code?

All you have to do include the "company\\" part in the username. You can either include the call the validate with the company domain: User_CustomValidate('username', 'password', 'company'); or set the default value for $domain in the function to handle this on its own: function User_CustomValidate(&$usr, &$pwd, $domain = 'company') {

This should do the trick:

// 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM