Created
September 25, 2020 14:35
-
-
Save ko31/6d3614835c446326c3481b99aeb7d244 to your computer and use it in GitHub Desktop.
A sample code of user authentication with LDAP from PHP
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 | |
// LDAP settings | |
const LDAP_HOST = "ldap-host"; | |
const LDAP_PORT = 389; | |
const LDAP_DC = "dc=example,dc=com"; | |
const LDAP_CN = "admin"; | |
const LDAP_PASS = "password"; | |
// Connect | |
$ldap_conn = ldap_connect(LDAP_HOST, LDAP_PORT); | |
if (!$ldap_conn) { | |
exit('Could not connect to LDAP server.'); | |
} | |
// Switch protocol to LDAPv3 | |
ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3); | |
ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0); | |
// Bind | |
$ldap_dn = 'cn=' . LDAP_CN . ',' . LDAP_DC; | |
$ldap_bind = ldap_bind($ldap_conn, $ldap_dn ,LDAP_PASS); | |
if ($ldap_bind) { | |
exit('LDAP bind successful..'); | |
} else { | |
exit('LDAP bind failed.'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment