Solved

How do I convert this ColdFusion page to PHP?

Posted on 2013-01-02
6
188 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
  • 3
  • 3
6 Comments
 
LVL 34

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 34

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

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

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now