How to authenticate via LDAP

sachin
edited July 2023 in PHP
$ldapServer = 'ldap://ldap_server.domain.me';
$ldapPort = 389;
$ldapBaseDn = 'cn=users,dc=ldap_server,dc=domain,dc=me'; // Replace with your base DN

// User provided credentials
$username = 'username';
$password = 'password@123';

// Connect to the LDAP server
$ldapConn = ldap_connect($ldapServer, $ldapPort);

if (!$ldapConn) {
    echo "Failed to connect to the LDAP server";
    exit;
}

// Set LDAP options
ldap_set_option($ldapConn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapConn, LDAP_OPT_REFERRALS, 0);

// Bind with the provided username and password
$ldapBind = @ldap_bind($ldapConn, "uid=$username,$ldapBaseDn", $password);

// Check if the bind was successful
if ($ldapBind) {
    echo "Authentication successful!";
    // Perform any further actions or grant access to the application
} else {
    echo "Authentication failed.";
}

ldap_unbind($ldapConn);
Tagged:
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!