Skip to content

Instantly share code, notes, and snippets.

@yasyf
Last active December 11, 2015 20:09
Show Gist options
  • Save yasyf/4653613 to your computer and use it in GitHub Desktop.
Save yasyf/4653613 to your computer and use it in GitHub Desktop.
<?php
/** {{{ http://code.activestate.com/recipes/101525/ (r1) */
$ldapconfig['host'] = 'localhost';
$ldapconfig['port'] = NULL;
$ldapconfig['basedn'] = 'dc=localhost,dc=com';
$ldapconfig['authrealm'] = 'My Realm';
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;
}
if (($result = ldap_authenticate()) == NULL) {
echo('Authorization Failed');
exit(0);
}
echo('Authorization success');
print_r($result);
/** end of http://code.activestate.com/recipes/101525/ }}} */
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment