Skip to content

Instantly share code, notes, and snippets.

@JBlond
Last active April 10, 2023 12:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JBlond/accd7b81a4ae3470daa1018f87b1f2b6 to your computer and use it in GitHub Desktop.
Save JBlond/accd7b81a4ae3470daa1018f87b1f2b6 to your computer and use it in GitHub Desktop.
LdapAuth php
<?php
$ldap = new LdapAuth('ldap://10.10.10.10');
if($ldap->auth($_POST['username'],$_POST['password'], 'example.local') === true){
echo 'Access granted';
}
<?php
/**
* LDAP Auth Handler
*/
class LdapAuth
{
/**
* @var false|resource
*/
private $handle;
/**
* @param string $server
*/
public function __construct(string $server='')
{
$this->handle = ldap_connect($server);
ldap_set_option($this->handle, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($this->handle, LDAP_OPT_REFERRALS, 0);
}
/**
* @param string $userName
* @param string $password
* @param string $domainSuffix e.g. example.local
* @return bool
*/
public function auth(string $userName, string $password, string $domainSuffix): bool
{
$bind = ldap_bind($this->handle, $userName . '@' . $domainSuffix, $password);
return $bind === true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment