Skip to content

Instantly share code, notes, and snippets.

@ko31
Created September 25, 2020 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ko31/6d3614835c446326c3481b99aeb7d244 to your computer and use it in GitHub Desktop.
Save ko31/6d3614835c446326c3481b99aeb7d244 to your computer and use it in GitHub Desktop.
A sample code of user authentication with LDAP from PHP
<?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