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
Solved

How do I convert this ColdFusion page to PHP?

Posted on 2013-01-02
6
191 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
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.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
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…

838 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