Jump to content

LDAP user lookup


Pioden

Recommended Posts

Hi folks,

 

I'm taking my first steps into the world of Ajax, and my first project is to write a little script to lookup usernames from our LDAP database. The idea is that when a user starts typing usernames they get offered a lst of potential answers. Standard ajax fayre I guess!

 

I've managed to make some headway but I could do with some help to finish it off. BTW if the code looks odd it's because this is all bastardised code from online tutorials shunted together to try and see what's going on!! :-)

 

OK the HTML bit ...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Photocopying</title>
</head>

<body>
<script language="javascript" type="text/javascript">
<!--

      // Get the HTTP Object

      function getHTTPObject(){

      if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");

      else if (window.XMLHttpRequest) return new XMLHttpRequest();

      else {

      alert("Your browser does not support AJAX.");

      return null;

      }

      }

       

      // Change the value of the outputText field

      function setOutput(){

      if(httpObject.readyState == 4){

      document.getElementById('outputText').value = httpObject.responseText;

      }


      }

      // Implement business logic

      function doWork(){

      httpObject = getHTTPObject();

      if (httpObject != null) {

      httpObject.open("GET", "scripts/ldapsearch.php?inputText="

      +document.getElementById('inputText').value, true);

      httpObject.send(null);

      httpObject.onreadystatechange = setOutput;

      }

      }


      var httpObject = null;
      //-->
</script>

<form name="ldapsearch">
<div id="username">Username: <input type="text" onkeyup="doWork();" name="inputText" id="inputText" /></div>

<div id="">Output text: <input type="text" name="outputText" id="outputText" /></div>
</form>
</body>
</html>

 

the php bit ...

 

<?php

if (isset($_GET['inputText']))

$username = trim($_GET['inputText']);

// LDAP variables
$ldaphost = "ldap.myplace.ac.uk";  // your ldap servers
$ldapport = 389;                 // your ldap server's port number

// Connecting to LDAP
$ds = ldap_connect($ldaphost, $ldapport) or die ("Could not connect to $ldaphost");

//Connection made -- bind anonymously and get dn for username.
    $bind = @ldap_bind($ds);
    
//Check to make sure we're bound.
    if( !bind )
    {
        echo "Anonymous bind to LDAP FAILED.  Contact Tech Services! (Debug 2)";
        TabBot();
        exit;
    }
    
$search =  ldap_search($ds, "dc=myplace,dc=ac,dc=uk", "uid=$username");
      
$info = ldap_get_entries($ds, $search);

print_r "$info";

?>

 

The issue at the moment is that the output of the search if an array. Do I loop the output as if it came from a database and then print it out at the end of the file? Am I on the right track? Sorry if these sound like really basic questions but Ajax is two hours new at the moment!!

 

TIA

 

Huw

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.