Solved

How do I convert this ColdFusion page to PHP?

Posted on 2013-01-02
6
196 Views
Last Modified: 2013-01-07
I have the attached CF script that connects to an LDAP server and displays a list of the users.

How can this be converted to PHP?
cf.txt
0
Comment
Question by:Donnie Walker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 38737100
Should be pretty simple in terms of syntax/language, so I'm guessing you're talking more about the Active Directory portion and not about how to dump it into the JSON format for the jQuery DataTables plugin.

If that's the case, then you'll basically need to use the LDAP extension for PHP. If you're on Windows, it should be easy to enable (if it's not already enabled), since the DLL is already there and you just have to enable / uncomment it in the php.ini file and restart PHP / the web server.

Here's the installation page for the extension:
http://php.net/manual/en/ldap.installation.php

Then you would just use ldap_connect() and ldap_bind() to connect to AD with the proper credentials, and ldap_search() to perform the query. Check this page for a full example:

http://php.net/manual/en/ldap.examples-basic.php
0
 

Author Comment

by:Donnie Walker
ID: 38737306
I am weak on PHP and LDAP.

Can you provide me with an example?
0
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 38738334
I can't really test it from here, but it would look like this:

echo "<h3>LDAP query test</h3>";
echo "Connecting ...";

$server		=	"domain.college.edu";
$username	=	"user@college.edu";
$password	=	"password;

$start		=	"dc=college,dc=edu";
$filter		=	"(&(objectCategory=Person)(objectClass=User)(employeeId=*)(mail=*)(|(memberOf=CN=ActiveGroup - Employees,OU=ActiveGroups,DC=college,DC=edu)(memberOf=CN=ActiveGroup - Students,OU=ActiveGroups,DC=college,DC=edu)))";
$attributes	=	"sAMAccountName,givenName,sn,telephoneNumber,title,department,physicalDeliveryOfficeName";

// Connect
$ds=ldap_connect($server);

if ($ds) { 

		// Bind
    $r=ldap_bind($ds, $username, $password);

		// Search
    $sr=ldap_search($ds, $start, $filter, $attributes);  
    // echo "Number of entries returned is " . ldap_count_entries($ds, $sr) . "<br />";

		// Loop through results and build the array
		$output = array("aaData" => array());
    $info = ldap_get_entries($ds, $sr);
    for ($i=0; $i<$info["count"]; $i++)
    {
    	$row = $info[$i];
    	$row["sAMAccountName"] = strtoupper($row["sAMAccountName"]);
    	$output["aaData"][] = $row;
    }
    
    // Encode the array to JSON
    echo json_encode($output);

		// Close the connection
    ldap_close($ds);

} else {
    echo "<h4>Unable to connect to LDAP server</h4>";
}

Open in new window

0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:Donnie Walker
ID: 38738371
When I put in my credentials it says:

Connecting ...{"aaData":[]}
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 38739701
Try print_r()-ing the different variables that get created, like this:

// Connect
$ds=ldap_connect($server);
print_r($ds);

Use the same method for anything that gets created from a ldap_ function.
0
 

Author Comment

by:Donnie Walker
ID: 38740747
When I do that for the $ds it says "Recource Id #2".

However, I found another script that seems to bind/search correctly and returns entries but runs out of memory. (see attached)

When I try to run it with the jquery datatables thing it says "DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error."

So, I assume the new attached script is not returning the correct values?

Sorry to be such a pain but I'm kind of learning as I go.
dir.txt
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question