PDA

View Full Version : PHP script to windows Ldap


ChronicleX.com
03-24-2005, 11:59 AM
we have scripts to do it but when we try them we get a line 7 error we are usering a windows webserver



http://asia.cnet.com/builder/architect/system/0,39009336,39105862-2,00.htm



<?php
// LDAP variables
$ldap[‘user’] = ‘uname’;
$ldap[‘pass’] = ‘password’;
$ldap[‘host’] = ‘ldap.example.com’;
$ldap[‘port’] = 389;
$ldap[‘dn’] = ‘cn’.$ldap[‘user’].’,ou=Department,o=Company Name’;
$ldap[‘base’] = ‘’;

// connecting to ldap
$ldap[‘conn’] = ldap_connect( $ldap[‘host’], $ldap[‘port’] )
or die( “Could not connect to {$ldap[‘host’]}” );
?>




we tried meny more

when we test ldap it works but when we use the php scripts it just gives an line 7 error. Hvae you guys ever tried anything like this before?

marek_mar
03-24-2005, 12:56 PM
What is on the lines artound line 7 (and on line 7) and what is the error msg?

ChronicleX.com
03-24-2005, 01:14 PM
hmmm i tried another script got the line 7 error agian i think it's to do with the way it connects to ldap



<?php
// basic sequence with LDAP is connect, bind, search, interpret search
// result, close connection

echo "<h3>LDAP query test</h3>";
echo "Connecting ...";
$ds=ldap_connect("localhost"); // must be a valid LDAP server!
echo "connect result is ".$ds."<p>";

if ($ds) {
echo "Binding ...";
$r=ldap_bind($ds); // this is an "anonymous" bind, typically
// read-only access
echo "Bind result is ".$r."<p>";

echo "Searching for (sn=S*) ...";
// Search surname entry
$sr=ldap_search($ds,"o=My Company, c=US", "sn=S*");
echo "Search result is ".$sr."<p>";

echo "Number of entires returned is ".ldap_count_entries($ds,$sr)."<p>";

echo "Getting entries ...<p>";
$info = ldap_get_entries($ds, $sr);
echo "Data for ".$info["count"]." items returned:<p>";

for ($i=0; $i<$info["count"]; $i++) {
echo "dn is: ". $info[$i]["dn"] ."<br>";
echo "first cn entry is: ". $info[$i]["cn"][0] ."<br>";
echo "first email entry is: ". $info[$i]["mail"][0] ."<p>";
}

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

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




ERROR

LDAP query test
Connecting ...
Fatal error: Call to undefined function: ldap_connect() in [url path]/ldap.php on line 7


our ldap is running fine when we use run on it. But when we try to access ldap by php we are unsure on what to put in the $ds=ldap_connect("localhost"); // must be a valid LDAP server! we tried (ldap.domain.local) and some others but they don't work????

mordred
03-24-2005, 01:45 PM
From the error message it looks like your PHP interpreter has not been compiled with LDAP support and/or this extension is not enabled. Check that it appears in the list of extensions shown after a call of phpinfo().

ChronicleX.com
03-24-2005, 02:00 PM
we did a php(); script and could not find anything saying ldap in it so does that mean we do don't have it intsalled???

mordred
03-24-2005, 02:08 PM
Exactly.

Try to enable the extension by uncommenting it in the php.ini file. There should be a line like this (roughly, out of my head):

;extension=php_ldap.dll

Remove the semi-colon. If that doesn't yield the expected results, you have to check your path to the extension directory and whether the php_ldap.dll (or whatever it is called) exists. Futher, it could be that this extension needs other dlls to find in the system. Check the manual for this issue.

ChronicleX.com
03-24-2005, 02:25 PM
ahhhh hmmm ok thanks

we are getting some where now

ldap
LDAP Support enabled
RCS Version $Id: ldap.c,v 1.130.2.10 2004/06/01 21:05:33 iliaa Exp $
Total Links 0/unlimited
API Version 2004
Vendor Name OpenLDAP
Vendor Version 0

ChronicleX.com
03-24-2005, 03:19 PM
Parse error: parse error in D:\Inetpub\wwwroot\robinphp\ldap.php on line 18



<?php
// basic sequence with LDAP is connect, bind, search, interpret search
// result, close connection

echo "<h3>LDAP query test</h3>";
echo "Connecting ...";
$ds=ldap_connect("our ip was put in here"); // must be a valid LDAP server!
echo "connect result is ".$ds."<p>";

if ($ds) {
echo "Binding ...";
$r=ldap_bind($ds); // this is an "anonymous" bind, typically
// read-only access
echo "Bind result is ".$r."<p>";

echo "Searching for (sn=S*) ...";
// Search surname entry
$sr=ldap_search($ds,"OU=Users,OU=Beeston,OU=J Tomlinson,DC=jtomlinson,DC=local"sn=s*");
echo "Search result is ".$sr."<p>";

echo "Number of entires returned is ".ldap_count_entries($ds,$sr)."<p>";

echo "Getting entries ...<p>";
$info = ldap_get_entries($ds, $sr);
echo "Data for ".$info["count"]." items returned:<p>";

for ($i=0; $i<$info["count"]; $i++) {
echo "dn is: ". $info[$i]["dn"] ."<br>";
echo "first cn entry is: ". $info[$i]["cn"][0] ."<br>";
echo "first email entry is: ". $info[$i]["mail"][0] ."<p>";
}

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

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

marek_mar
03-24-2005, 03:33 PM
Too many quotes in

$sr=ldap_search($ds,"OU=Users,OU=Beeston,OU=J Tomlinson,DC=jtomlinson,DC=local"sn=s*");

ChronicleX.com
03-24-2005, 03:36 PM
so what would i need to take out am A PHP NOOBIE

ChronicleX.com
03-24-2005, 04:24 PM
i tried every thing am very confused. With this now getting me mad :mad: stupid thing don't work.

mordred
03-24-2005, 04:51 PM
Take a deep breath, reread the manual page and then you eventually see that you missed a comma and a double quote to seperate the parameters in your ldap_search() call.


$sr=ldap_search($ds,"OU=Users,OU=Beeston,OU=J Tomlinson,DC=jtomlinson,DC=local", "sn=s*");


Does that help?

marek_mar
03-25-2005, 12:27 AM
Too many quotes in

$sr=ldap_search($ds,"OU=Users,OU=Beeston,OU=J Tomlinson,DC=jtomlinson,DC=local"sn=s*");

That means you have to cut one of the quotes out,

$sr=ldap_search($ds,"OU=Users,OU=Beeston,OU=J Tomlinson,DC=jtomlinson,DC=local,sn=s*");
Try that.

ChronicleX.com
04-11-2005, 12:30 PM
Thanks my boss got it working while i was on holiday :D