Solved

How do I convert this ColdFusion page to PHP?

Posted on 2013-01-02
6
190 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

831 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