[KLUG Members] LDAP & PHP

Adam Williams members@kalamazoolinux.org
04 Aug 2003 18:59:53 -0400


> I want to write the auth myself. 

The typical example code is -

function ldap_authenticate() {
    global $ldapconfig;
    global $PHP_AUTH_USER;
    global $PHP_AUTH_PW;
    
    if ($PHP_AUTH_USER != "" && $PHP_AUTH_PW != "") {
        $ds=@ldap_connect($ldapconfig['host'],$ldapconfig['port']);
        $r = @ldap_search( $ds, $ldapconfig['basedn'], 'uid=' . $PHP_AUTH_USER);
        if ($r) {
            $result = @ldap_get_entries( $ds, $r);
            if ($result[0]) {
                if (@ldap_bind( $ds, $result[0]['dn'], $PHP_AUTH_PW) ) {
                    return $result[0];
                }
            }
        }
    }
    header('WWW-Authenticate: Basic realm="'.$ldapconfig['authrealm'].'"');
    header('HTTP/1.0 401 Unauthorized');
    return NULL;
}

> I don't want to use htaccess.
> I want users to login so I have their information. Not neccisarily for
> security.

You can still have the information if you use htaccess. Apache stores
the name of the athenticated user in a variable accessible to PHP.

$username = getenv("REMOTE_USER");