Hi I have a PHP script that searches my AD, but how can I make it search more than one DN. I've tried the exmample on php.net and cannot get it to work.
Help appreciated:
<?php echo "<?xml version='1.0' encoding='utf-8' ?>" ?><?php echo "<ul class='LSRes'>" ?>
<?php
if( isset($_GET['q']) &&!empty($_GET['q']) ){
// all your ldap code
// Designate a few variables
$host = "10.10.10.10"; // Add in your AD host name or IP
$user = "DOMAIN\user"; // Add in your AD access account user name
$pswd = "password"; // Add in your AD access account user name password
$ds = ldap_connect($host)
or die( "Could not connect!" );
// Set version number
ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3)
or die ("Could not set ldap protocol");
// Binding to ldap server
$bd = ldap_bind($ds, $user, $pswd)
or die ("Could not bind");
// Create the DN - Add in the OU of your AD
$dn[] = "OU=uk,OU=Accounts,DC=mywebsite,DC=com";
$dn[] = "OU=us,OU=Accounts,DC=mywebsite,DC=com";
$id[] = $ds;
$id[] = $ds;
//$filter = 'samaccountname='.$_POST['username'];
$filter = "(|(givenName=".$_GET['q']."*) (sn=".$_GET['q']."*) (displayname=".$_GET['q']."*) (samaccountname=".$_GET['q']."*))";
$result = ldap_search($id,$dn,$filter);
$search = false;
foreach ($result as $value) {
if(ldap_count_entries($ds,$value)>0){
$search = $value;
break;
}
}
if($search){
$entries = ldap_get_entries($ds, $search);
}
if ($entries["count"] > 0) {
for ($i=0; $i<$entries["count"]; $i++) {
echo "<span class='LSstyle'>Name: <strong>".$entries[$i]["displayname"][0]." ".$entries[$i]["sn"][0]."</strong></span><br />";
echo "<span class='LSstyle'>Short name: <strong>".$entries[$i]["samaccountname"][0]."</strong></span><br />";
echo "<span class='LSstyle'>Phone: <strong>".$entries[$i]["telephonenumber"][0]."</strong></span><br />";
echo "<span class='LSstyle'>Title: <strong>".$entries[$i]["title"][0]."</strong></span><br />";
echo "<span class='LSstyle'>Dept: <strong>".$entries[$i]["department"][0]."</strong></span></p>";
}
} else {
echo "<span class='LSstyle_noresults'><strong>No results found</strong></span>";
}
ldap_unbind($ad);
}
?>
Seeing as ldap_search does not take an array as $base_dn, you will probably have to loop $dn as Viper_Sb suggested.
Related
When creating the user I cannot start the section with the user's password. The user is created, but when testing the connection, it marks invalid credentials. Can you tell me where I am wrong? How should the password be encrypted?
$ds = '192.168.1.10';
$portldap = 389;
$ldap_username = 'CN=Administrador,CN=Users,DC=Local,DC=com';
$ldap_password = 'rootadm1';
$ldap_password_user = $ldap_password;
$ldap_connection = ldap_connect($ds, $portldap);
if($ldap_connection ){
// We have to set this option for the version of Active Directory we are using.
ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3) or die('Unable to set LDAP protocol version');
ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0); // We need this for doing an LDAP search.
$r = ldap_bind($ldap_connection, $ldap_username, $ldap_password);
if($r === TRUE){
$CN = htmlspecialchars($_POST["cn"]);
$ldaprecord['cn'] = $CN;
$ldaprecord['sAMAccountName'] = $_POST["sAMAccountName"]; //20caract
$ldaprecord['userPrincipalName'] = $_POST["userprincipalname"];
$ldaprecord["objectClass"][0] = "top";
$ldaprecord["objectClass"][1] = "person";
$ldaprecord["objectClass"][2] = "organizationalPerson";
$ldaprecord["objectClass"][3] = "user";
$ldaprecord['description']= $_POST["description"];
$ldaprecord["userAccountControl"][0] = "66656";
$ldap_new = 'CN='.$CN.',CN=Users,DC=Local,DC=com';
$result = ldap_add($ldap_connection, $ldap_new, $ldaprecord);
if($result) {
//Success creating user - Add password
$addPrecord['userPassword'] = '{MD5}' . base64_encode(pack('H*',md5($ldap_password_user)));
//$addPrecord["unicodePwd"][0] = iconv( 'UTF-8', 'UTF-16LE', $ldap_password_user );
$add_record= ldap_modify($ldap_connection, $ldap_new, $addPrecord);
if($add_record){
ldap_close($ldap_connection);
return $result;
}
else {
echo "LDAP Error: ".ldap_error($ldap_connection)."\n";
exit;
}
}
else {
echo "LDAP Error: ".ldap_error($ldap_connection)."\n";
exit;
}
}
}
else {
echo "cannot connect to LDAP server at $ds.";
} ```
[enter image description here][1]
[1]: https://i.stack.imgur.com/QOzqZ.png
I was sent the following LDAP parameters, but am not sure how to establish a connection in PHP. I'm not sure which PHP function to use with each set of parameters. Here are the parameters I was given:
Server: ldaps://the_server.com:636
root DN: dc=the_info,dc=more_info,dc=com
User search base: ou=CompanyUsers
User search filter: sAMAccountName={0}
Group search base: OU=Security,OU=CompanyGroups
Group search filter: cn={0}
Group membership: Group membership attribute = memberOf
Display Name LDAP attribute: displayname
Email Address LDAP atribute: mail
If someone could provide a php script for me that would be great! This is my first time using LDAP and still do not understand all these parameters.
Following is the working code for linux base ldap.
It might be helpful to you.
<?php
$username = 'uid=amitkawasthi,ou=CompanyUsers,dc=the_info,dc=more_info,dc=com';
$password= 'test';
$ds=ldap_connect("the_server.com, 636");
echo $ds;
if ($ds) {
echo "Binding ...";
$r=ldap_bind($ds, $username, $password);
if ($r)
{
$sr=ldap_search($ds,"ou=CompanyUsers,dc=the_info,dc=more_info,dc=com", "uid=amitkawasthi");
$entry = ldap_first_entry($ds, $sr);
$attrs = array();
$attribute = ldap_first_attribute($ds,$entry,$identifier);
while ($attribute) {
$attrs[] = $attribute;
$attribute=ldap_next_attribute($ds,$entry,$identifier);
}
echo count($attrs) . " attributes held for this entry:<p>";
$ldapResults = ldap_get_entries($ds, $sr);
//for ($item = 0; $item < $ldapResults['count']; $item++) {
// for ($attribute = 0; $attribute < $ldapResults[$item]['count']; $attribute++) {
//echo $data = $ldapResults[$item][$attribute];
echo $data = $ldapResults[0][$attribute];
echo $data.": ".$ldapResults[0][$data][0]."<br>";
//}
///echo '<hr />';
echo "OK";
}
else
{
echo "Fail";
}
}
?>
============================
I am trying to get information out of AD using LDAP query and PHP. Below is my code and it seems like nothing returns from this code. I have an issue with code block in if (TRUE === $bind). Would anyone help me to point out the error?
This is php code for listing all users in AD and showing up on WordPress page.
$ldap_password = "pass";
$ldap_username = "username";
$person = "Scott";
$ldap_connection = ldap_connect("host1");
if (FALSE === $ldap_connection) {
echo "Connection is failed<br />";
}
else {
echo "Connection is successful<br />";
}
ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3) or die("Unable to set LDAP protocol version");
ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0);
$bind = ldap_bind($ldap_connection, $ldap_username, $ldap_password);
if (TRUE === $bind) {echo "Binding is successful<br />";}
else {echo "Binding is unsuccessful<br />";}
if (TRUE === $bind) {
echo "Retrieving...<br />";
$baseDN = "dc=domain,dc=com";
$filter="(|(sn=$person*)(givenname=$person*))";
$justthese = array("ou");
$sr = ldap_list($ldap_connection, $baseDN, $filter, $justhese);
$info = ldap_get_entries($ldap_connection,$sr);
echo $info["count"]."Hello<br />";
for ($i=0; $i < $info["count"]; $i++) {
echo $info[$i]["ou"][0];
}
ldap_unbind($ldap_connection);
echo "Unbinding is completed<br />";
}
This is what shows up on the page
Connection is successful
Binding is successful
Retrieving…
Hello
Unbinding is completed
Thanks
I am new to PHRETS. And need to build one search panel which could search and display using PHRets and Rets Server. Here its getting connected but doesn't show results. I want something that can show results nicely.
SEARCH.PHP
<?php
$rets_login_url = "http://connectmls-rets.mredllc.com/rets/server/login";
$rets_username = "xxxxxxxx";
$rets_password = "xxxxxxxx";
require_once("phrets.php");
$rets = new phRETS;
//echo "+ Connecting to {$rets_login_url} as {$rets_username}<br>\n";
$connect = $rets->Connect($rets_login_url, $rets_username, $rets_password);
//echo "<pre>";
//print_r($rets);
//echo "</pre>";
if ($connect) {
echo "<p class=\"success\">Connected</p>";
////////////////////////////////////////////////////////
$search = $rets->SearchQuery("Property","ResidentialProperty","(ListDate=1990-01-01+)",array("StandardNames" => 1));
while ($listing = $rets->FetchRow($search)) {
echo "Address: {$listing['StreetNumber']} {$listing['StreetName']}, ";
echo "{$listing['City']}, ";
echo "{$listing['State']} {$listing['ZipCode']} listed for ";
echo "\$".number_format($listing['ListPrice'])."\n";
}
$rets->FreeResult($search);
////////////////////////////////////////////////////////
}
else {
echo "<p class=\"error\">Not connected:<p>";
print_r($rets->Error());
exit;
}
if (!empty($rets->err)) {
echo "HTTP {$rets->err}\n";
}
$last_req = $rets->LastRequest();
if ($last_req['ReplyCode'] != 0) {
echo "RETS {$last_req['ReplyCode']}: {$last_req['ReplyText']}\n";
}
//echo "Server reports: ".$rets->GetServerVersion()."\n";
?>
NOT SHOWING RESULTS. PLEASE HELP AS SOON AS POSSIBLE.
Try this
$search = $rets->SearchQuery("Property","ResidentialProperty",
"(ModificationTimestamp=2011-01-01T00:00:00+) ,
(ListPrice=300000)", array("Limit" => "20",
"Format" => "COMPACT","StandardNames" => "1","Count"=>1) );
The title may be misleading. I apologize for that.
Anyways, I am confused about SQL and ODBC.
On my website, I am trying to setup a register page.
Basically, on my dedicated server with MYSQL running on it, I setup some register.php files so that when a person registers on the site, it gets put into a certain database in SQL. When I try to register, I get "odbc not connected" Do I need to add a new data source?
Example of the html/php file: "Register.php"
<?php
$aw=$_GET["q"];
$aws=$_GET["q2"];
$awts=$_GET["q3"];
$conn = odbc_connect('USER_MEMBERDB','Test','Test123');
//$sql="SELECT * FROM chr_log_info WHERE id_loginid = '$q'";
if ($conn)
{
$query = "select * from chr_log_info where id_loginid = '$aw'";
//perform the query
$result=odbc_exec($conn, $query)or die("Error Here!");;
// $aw = mssql_escape_string($aw);
// $aws = mssql_escape_string($aws);
$slashRead1 = "";
$slashRead2 = "";
for($i = 0; $i < strlen($aw); $i++){
if($aw[$i] == '\\'){
$slashRead1 = 'yah';
}
}
for($i = 0; $i < strlen($aws); $i++){
if($aws[$i] == '\\'){
$slashRead2 = 'yah';
}
}
if(!empty($slashRead1) && !empty($slashRead2)){
echo "Username and Password values cannot be accepted. Please change your Username and Password!";
}else if(!empty($slashRead1)){
echo "Username value cannot be accepted. Please change your Username!";
}else if(!empty($slashRead2)){
echo "Password value cannot be accepted. Please change your Password!";
}else{
if(empty($aws)){
echo "Please fill-in the form completely!";
}else{
$count = 0;
//fetch tha data from the database by row
while(odbc_fetch_row($result)){
for($i=1;$i<=odbc_num_fields($result);$i++){
$row = odbc_result($result, $i);
if(!empty($row)){
$count++;
}
}
}
if(empty($aw)){
echo "Please fill-in the form completely!";
}else if($count != 0){
echo "Sorry, the username you entered is not available!";
}else if(strlen($aws) < 6){
echo "Please enter more than 6 characters for your password!";
}else{
$query1 = "Select id_idx from chr_log_info";
$result1=odbc_exec($conn, $query1)or die("Error Here!");;
while(odbc_fetch_row($result1)){
for($i=1;$i<=odbc_num_fields($result1);$i++){
$row = odbc_result($result1, $i);
}
}
$row++;
$query2 = "Select propid from chr_log_info";
$result2=odbc_exec($conn, $query2)or die("Error Here!");;
while(odbc_fetch_row($result2)){
for($i=1;$i<=odbc_num_fields($result2);$i++){
$row2 = odbc_result($result2, $i);
}
}
$row2++;
$query3 = "Insert into chr_log_info(id_idx, propid, id_loginid, id_passwd, id_sexType) values('$row', '$row2', '$aw', '$aws', '$awts')";
$result=odbc_exec($conn, $query3) or die("Error Here!");
echo "Congratulations! You have successfully registered!";
}
}
}
//close the connection
odbc_close ($conn);
}
else echo "odbc not connected";
?>
Another HTML file that is used is this: "responsexml.html"
<?php
$q=$_GET["q"];
$conn = odbc_connect('USER_MEMBERDB','Test','Test123');
if ($conn)
{
$q = mysql_escape_string($q);
$slashRead = "";
for($i = 0; $i < strlen($q); $i++){
if($q[$i] == '\\'){
$slashRead = 'yah';
}
}
if(!empty($slashRead)){
echo "Username value cannot be accepted!";
}else{
//the SQL statement that will query the database
$query = "select * from chr_log_info where id_loginid = '$q'";
//perform the query
$result=odbc_exec($conn, $query) or die("Wrong!");
//fetch tha data from the database
$count = 0;
while($row = odbc_fetch_row($result))
{
for($i=1;$i<=odbc_num_fields($result);$i++)
{
$row = odbc_result($result, $i);
if(!empty($row)){
$count++;
}
}
}
if(empty($q)){
echo "";
}else if($count != 0){
echo "Sorry, the username is not available!";
}else{
echo "Username is available!";
}
}
//close the connection
odbc_close ($conn);
}
else echo "odbc not connected";
?>
Am I missing something? Or do I need to download certain drivers for my dedicated server for it to run. Any suggestions?
My assumption is that USER_MEMBERDB that is in your odbc_connect is the name of the MSSQL database that you are trying to connect to.
You could try:
// Microsoft SQL Server using the SQL Native Client 10.0 ODBC Driver - allows connection to SQL 7, 2000, 2005 and 2008
$connection = odbc_connect("Driver={SQL Server Native Client 10.0};Server=$server;Database=$database;", $user, $password);
However, I would recommend using the built in MSSQL functions over ODBC for this purpose:
$conn = mssql_connect('SERVER', 'username', 'password');
http://php.net/manual/en/ref.mssql.php