I am trying to use LDAP authentication using PHP.
Below is my code:
<?php
$ldaphost = 'ldap://ldapServer';
$ldapport = 389;
$ds = ldap_connect($ldaphost, $ldapport)
or die("Could not connect to $ldaphost");
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
//ldap_set_option($ds, LDAP_OPT_DEBUG_LEVEL, 7);
if ($ds)
{
$username = "[email protected]";
$upasswd = "testpass";
$ldapbind = ldap_bind($ds, $username, $upasswd);
if ($ldapbind)
{print "Congratulations! $username is authenticated.";}
else
{print "Access Denied!";}
}
?>
But it raises the below error:
PHP Warning:
ldap_bind(): Unable to bind to server: Can't contact LDAP server
Any idea as how can I get it resolved?
Note: Do we need ldap.config file somewhere as I came across this term on some forum. I don't see any such file on my machine. I have php_ldap.dll in ext folder and using Windows.
mod_auth_ldap? This will do everything for you.ldap_connect()does not require theldap://wrapper, it automatically assumes you are connecting to an LDAP server, you only need to specify a protocol if you are usingldaps://. Try just supplying the hostname/IP address. Also, verify that the LDAP server is able to accept connections from external machines on TCP/UDP 389 (check the firewall and the binding address of the server). You should not needldap.config. Note thatldap_connect()- despite the name - does not initiate a connection to the server, it only prepares the resource for use.