Page 1 of 1

PHP Search LDAP server

Posted: Sat Sep 03, 2011 4:03 pm
by asai
I think this is the correct room for this question...
I've been trying to make a script for searcing my LDAP server.
I have success in connecting and listing the content, but something is wrong in my code for searching.

Here is my input html file:

Code: Select all

<html>
<head>
<title>Search</title>
</head>
<body>
<form action="search.php" method="POST">
<input type="text" name="name" length="30">
<input type="submit" name="submit" value = "Søk">
</form>
</body>
</html>
And the search.php:

Code: Select all

<?php
$ds=ldap_connect("ldap://10.10.160.4");
ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3);

if ($ds) { 
    $r=ldap_bind($ds);
 
    $query = "(cn=" . $_POST['name'] . ")";
    $sr=ldap_search($ds, "dc=dyndns,dc=org", "(cn=*)");  

    echo "Number of entries: " . ldap_count_entries($ds, $sr) . "<br />";

    $info = ldap_get_entries($ds, $sr);

    for ($i=0; $i<$info["count"]; $i++) {
        echo "Navn: " . $info[$i]["cn"][0] . "<br />";
        echo "Telefon: " . $info[$i]["telephoneNumber"][0] . "<br />";
        echo "Mobil: " . $info[$i]["mobile"][0] . "<br />";
        echo "E-post: " . $info[$i]["mail"][0] . "<br /><hr />";
    }

    echo "Closing connection";
    ldap_close($ds);

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

Re: PHP Search LDAP server

Posted: Sun Sep 04, 2011 12:34 am
by ok
When you run this script, what is the output you get?

Re: PHP Search LDAP server

Posted: Sun Sep 04, 2011 2:11 am
by asai
I get the hole content of the server... :roll:

Re: PHP Search LDAP server

Posted: Sun Sep 04, 2011 7:48 am
by ok
I am not really sure but don't you want:

Code: Select all

<?php
// ...
ldap_search($ds, "dc=dyndns,dc=org", $query);  
Instead of:

Code: Select all

<?php
// ...
ldap_search($ds, "dc=dyndns,dc=org", "(cn=*)");  

Re: PHP Search LDAP server

Posted: Sun Sep 04, 2011 8:04 am
by asai
That actually work! Thanks. :D

However, it only shows the record if the search string is equal the CN.
Do you know the code for using * with the search? E.g. if I want every record with givenname Bill?

Re: PHP Search LDAP server

Posted: Sun Sep 04, 2011 8:08 am
by ok
Maybe:

Code: Select all

<?php
// ...
ldap_search($ds, "dc=dyndns,dc=org", $query . "*");  

Re: PHP Search LDAP server

Posted: Sun Sep 04, 2011 8:30 am
by asai
Sorry, I found the solution: In the search string I only type *Bill* and every record containing Bill is displayed. :)

Re: PHP Search LDAP server

Posted: Sun Sep 04, 2011 10:07 am
by asai
Another question to my script: How can I get the output to a table?

Re: PHP Search LDAP server

Posted: Sun Sep 04, 2011 10:17 am
by ok
An HTML table? Database table?

Re: PHP Search LDAP server

Posted: Sun Sep 04, 2011 10:19 am
by asai
A html table.
One other thing: Is it possible to make the e-mail records like links?

Re: PHP Search LDAP server

Posted: Sun Sep 04, 2011 10:24 am
by ok
Well, your question is pure HTML...

Anyway, for the email thing you would replace:

Code: Select all

 echo "E-post: " . $info[$i]["mail"][0] . "<br /><hr />";
with:

Code: Select all

 echo "E-post: <a href='mailto:" . $info[$i]["mail"][0] . "'>" . $info[$i]["mail"][0] . "</a><br /><hr />";
For the HTML table you would replace:

Code: Select all

for ($i=0; $i<$info["count"]; $i++) {
        echo "Navn: " . $info[$i]["cn"][0] . "<br />";
        echo "Telefon: " . $info[$i]["telephoneNumber"][0] . "<br />";
        echo "Mobil: " . $info[$i]["mobile"][0] . "<br />";
        echo "E-post: " . $info[$i]["mail"][0] . "<br /><hr />";
}
with:

Code: Select all

echo '<table><tr><th>Navn</th><th>Telefon</th><th>Mobil</th><th>E-post</th></tr>';
for ($i=0; $i<$info["count"]; $i++) {
        echo '<tr>';
        echo "<td>" . $info[$i]["cn"][0] . "</td>";
        echo "<td>" . $info[$i]["telephoneNumber"][0] . "</td>";
        echo "<td>" . $info[$i]["mobile"][0] . "</td>";
        echo "<td>" . $info[$i]["mail"][0] . "</td>";
        echo '</tr>';
}
echo '</table>';

Re: PHP Search LDAP server

Posted: Sun Sep 04, 2011 10:32 am
by asai
Very, very nice. Thanks for very good help.
One last question: Is there a way to align the titles and/or set the column width in the table?

Re: PHP Search LDAP server

Posted: Sun Sep 04, 2011 10:37 am
by ok
Yes. Use the width and text-align properties in the style attribute:
http://www.w3schools.com/cssref/pr_dim_width.asp
http://www.w3schools.com/cssref/pr_text_text-align.asp

Re: PHP Search LDAP server

Posted: Sun Sep 04, 2011 11:11 am
by asai
Works perfect. Thanks again ok. Very helpfull! :D