Connect to ldap server php

Following sample PHP code will connect to your local (or remote) Active Directory Domain Controller (LDAP server) and return all object with specified OU:

  • Number of objects found
  • Common Name
  • Distinguished Name
  • Description (if exists)
  • Primary Email Address (if exists)

Be aware that LDAP support by default is not installed in PHP. For instructions on how to install it check here.

 
<?php
// -------------- CHANGE VARIABLES TO SUIT YOUR ENVIRONMENT  --------------
//LDAP server address
$server = "ldap://192.168.1.55";
//domain user to connect to LDAP
$user = "This email address is being protected from spambots. You need JavaScript enabled to view it.";
//user password
$psw = "password";
//FQDN path where search will be performed. OU - organizational unit / DC - domain component
$dn = "OU=Accounts,OU=My Company,DC=mydomain,DC=com";
//Search query. CN - common name (CN=* will return all objects)
$search = "CN=*";                    
// ------------------------------------------------------------------------
echo "<h2>php LDAP query test</h2>";
// connecting to LDAP server
$ds=ldap_connect($server);
$r=ldap_bind($ds, $user , $psw); 
// performing search
$sr=ldap_search($ds, $dn, $search);
$data = ldap_get_entries($ds, $sr);    
echo "Found " . $data["count"] . " entries";
for ($i=0; $i<$data["count"]; $i++) {
 echo "<h4><strong>Common Name: </strong>" . $data[$i]["cn"][0] . "</h4><br />";
 echo "<strong>Distinguished Name: </strong>" . $data[$i]["dn"] . "<br />";
 //checking if discription exists 
 if (isset($data[$i]["description"][0])) 
 echo "<strong>Desription: </strong>" . $data[$i]["description"][0] . "<br />";
 else 
 echo "<strong>Description not set</strong><br />";
 //checking if email exists
 if (isset($data[$i]["mail"][0]))
 echo "<strong>Email: </strong>" . $data[$i]["mail"][0] . "<br /><hr />";
 else 
 echo "<strong>Email not set</strong><br /><hr />";
 }
 // close connection
 ldap_close($ds);
?>
 

How do I connect to LDAP server?

Add a server profile..
Go to File > New > New Profileā€¦.
Enter a name for the profile, such as Google LDAP..
Click Next. Enter the following: Host: ldap.google.com. Port: 636. Base DN: Your domain name in DN format. ( eg. ... .
Click Next..
Select External (SSL Certificate)..
Click Next..
Click Finish..

How do I access my LDAP server from my browser?

Select Start > Run, type ldp.exe, and then select OK. Select Connection > Connect. In Server and in Port, type the server name and the non-SSL/TLS port of your directory server, and then select OK. For an Active Directory Domain Controller, the applicable port is 389.

How configure LDAP in PHP?

Install php ldap extension (If not installed yet) For Debian, the installation command would be apt-get install php-ldap. For RHEL based systems, the command would be yum install php-ldap..
Search for extension=php_ldap.so in php. ini file..

How can I tell if PHP supports LDAP?

You can try and check it with extension_loaded() like this: $builtWithLdap = extension_loaded('ldap'); or alternativly as a crowbar approach, just check if one of the functions exists: $builtWithLdap = function_exists('ldap_add');