Below is the code which can be used to retreive the Active directory content using PHP
<?php
$ldap_server = "ldap://SERVERNAME";
$auth_user = "MASIN\\UserName";
$auth_pass = "Password";
// Set the base dn to search the entire directory.
$base_dn = "DC=MASIN, DC=com";
// Show only user persons
$filter = "(&(objectClass=user)(objectCategory=person)(cn=*))";
// Enable to show only users
// $filter = "(&(objectClass=user)(cn=$*))";
// Enable to show everything
// $filter = "(cn=*)";
// connect to server
if (!($connect=@ldap_connect($ldap_server))) {
die("Could not connect to ldap server");
}
ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
// bind to server
if (!($bind=@ldap_bind($connect, $auth_user, $auth_pass))) {
die("Unable to bind to server");
}
//if (!($bind=@ldap_bind($connect))) {
// die("Unable to bind to server");
//}
// search active directory
if (!($search=@ldap_search($connect, $base_dn, $filter))) {
die("Unable to search ldap server");
}
$number_returned = ldap_count_entries($connect,$search);
$info = ldap_get_entries($connect, $search);
echo "The number of entries returned is ". $number_returned."<p>";
for ($i=0; $i<$info["count"]; $i++) {
echo "Name is: ". $info[$i]["name"][0]."<br>";
echo "Display name is: ". $info[$i]["displayname"][0]."<br>";
echo "Email is: ". $info[$i]["mail"][0]."<br>";
echo "Telephone number is: ". $info[$i]["telephonenumber"][0]."<p>";
}
unset($connect) ;
unset($bind) ;
?>
<?php
$ldap_server = "ldap://SERVERNAME";
$auth_user = "MASIN\\UserName";
$auth_pass = "Password";
// Set the base dn to search the entire directory.
$base_dn = "DC=MASIN, DC=com";
// Show only user persons
$filter = "(&(objectClass=user)(objectCategory=person)(cn=*))";
// Enable to show only users
// $filter = "(&(objectClass=user)(cn=$*))";
// Enable to show everything
// $filter = "(cn=*)";
// connect to server
if (!($connect=@ldap_connect($ldap_server))) {
die("Could not connect to ldap server");
}
ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
// bind to server
if (!($bind=@ldap_bind($connect, $auth_user, $auth_pass))) {
die("Unable to bind to server");
}
//if (!($bind=@ldap_bind($connect))) {
// die("Unable to bind to server");
//}
// search active directory
if (!($search=@ldap_search($connect, $base_dn, $filter))) {
die("Unable to search ldap server");
}
$number_returned = ldap_count_entries($connect,$search);
$info = ldap_get_entries($connect, $search);
echo "The number of entries returned is ". $number_returned."<p>";
for ($i=0; $i<$info["count"]; $i++) {
echo "Name is: ". $info[$i]["name"][0]."<br>";
echo "Display name is: ". $info[$i]["displayname"][0]."<br>";
echo "Email is: ". $info[$i]["mail"][0]."<br>";
echo "Telephone number is: ". $info[$i]["telephonenumber"][0]."<p>";
}
unset($connect) ;
unset($bind) ;
?>
No comments:
Post a Comment