Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.
Moderator: General Moderators
asai
Forum Commoner
Posts: 43 Joined: Tue May 04, 2010 6:24 am
Location: Norway
Post
by asai » Sat Sep 03, 2011 4:03 pm
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>";
}
?>
ok
Forum Contributor
Posts: 393 Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land
Post
by ok » Sun Sep 04, 2011 12:34 am
When you run this script, what is the output you get?
asai
Forum Commoner
Posts: 43 Joined: Tue May 04, 2010 6:24 am
Location: Norway
Post
by asai » Sun Sep 04, 2011 2:11 am
I get the hole content of the server...
ok
Forum Contributor
Posts: 393 Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land
Post
by ok » Sun Sep 04, 2011 7:48 am
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=*)");
asai
Forum Commoner
Posts: 43 Joined: Tue May 04, 2010 6:24 am
Location: Norway
Post
by asai » Sun Sep 04, 2011 8:04 am
That actually work! Thanks.
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?
ok
Forum Contributor
Posts: 393 Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land
Post
by ok » Sun Sep 04, 2011 8:08 am
Maybe:
Code: Select all
<?php
// ...
ldap_search($ds, "dc=dyndns,dc=org", $query . "*");
asai
Forum Commoner
Posts: 43 Joined: Tue May 04, 2010 6:24 am
Location: Norway
Post
by asai » Sun Sep 04, 2011 8:30 am
Sorry, I found the solution: In the search string I only type *Bill* and every record containing Bill is displayed.
asai
Forum Commoner
Posts: 43 Joined: Tue May 04, 2010 6:24 am
Location: Norway
Post
by asai » Sun Sep 04, 2011 10:07 am
Another question to my script: How can I get the output to a table?
ok
Forum Contributor
Posts: 393 Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land
Post
by ok » Sun Sep 04, 2011 10:17 am
An HTML table? Database table?
asai
Forum Commoner
Posts: 43 Joined: Tue May 04, 2010 6:24 am
Location: Norway
Post
by asai » Sun Sep 04, 2011 10:19 am
A html table.
One other thing: Is it possible to make the e-mail records like links?
ok
Forum Contributor
Posts: 393 Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land
Post
by ok » Sun Sep 04, 2011 10:24 am
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>';
asai
Forum Commoner
Posts: 43 Joined: Tue May 04, 2010 6:24 am
Location: Norway
Post
by asai » Sun Sep 04, 2011 10:32 am
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?
ok
Forum Contributor
Posts: 393 Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land
Post
by ok » Sun Sep 04, 2011 10:37 am
asai
Forum Commoner
Posts: 43 Joined: Tue May 04, 2010 6:24 am
Location: Norway
Post
by asai » Sun Sep 04, 2011 11:11 am
Works perfect. Thanks again ok. Very helpfull!