Created
October 10, 2019 16:25
-
-
Save hxtree/75659581f568d8fb530da4e8be750c04 to your computer and use it in GitHub Desktop.
PHP LDAP Quick Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$uid = 'username'; | |
$password = 'password'; | |
echo 'Try login as uid "' . $uid . '" using password [redacted]<br><br>' . PHP_EOL; | |
// connect | |
$ldapconn = ldap_connect('ldap://ldap.example.com'); | |
if (!$ldapconn) { | |
echo 'Could not connect to authentication server.' . PHP_EOL; | |
die(); | |
} | |
// authenticatie user | |
$ldaprdn = 'uid=' . $uid . ',ou=people,dc=example,dc=com'; | |
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3); | |
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0); | |
ldap_start_tls($ldapconn); | |
$ldapbind = @ldap_bind($ldapconn, $ldaprdn, $password); | |
if (!$ldapbind) { | |
echo 'Invalid username or password.' . PHP_EOL; | |
die(); | |
} | |
// lookup info | |
$filter='(uid='.$uid.')'; | |
$results = ldap_search($ldapconn,'dc=example,dc=com',$filter); | |
ldap_sort($ldapconn,$results,'sn'); | |
$info = ldap_get_entries($ldapconn, $results); | |
// display info | |
for ($i=0; $i<$info['count']; $i++) { | |
echo 'Success' . PHP_EOL; | |
echo 'uid: ' . $info[$i]['uid'][0] . PHP_EOL; | |
echo 'name: ' . $info[$i]['cn'][0]. PHP_EOL; | |
echo 'mail: ' . $info[$i]['mail'][0]. PHP_EOL; | |
echo 'telephonenumber: ' . $info[$i]['telephonenumber'][0]. PHP_EOL; | |
echo 'title: ' . $info[$i]['title'][0]. PHP_EOL; | |
break; | |
} | |
// close ldap connection | |
@ldap_close($ldapconn); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment