Jump to content

Recommended Posts

Posted

I have an apache 2.2.11 server with php 5.2.9 and mysql 5.1.34 running on 64bit windows server 2k3. i have enabled the ldap extensions within php, but am not sure on how to use the ldap stuff, what i would like it to do is to pick up the users system login details and display their username.

 

once i have got that i would like to deny access to pages depending on which ad group they are in, which i don't know how to do either.

Guest leeneilson
Posted

Here is a PHP example script which hopefully should get you sorted.

 


/* CHANGE THESE */
   $ldapBaseDomain = 'ou=users,dc=example,dc=com';
   $ldapServer = 'LDAP_SERVER';
   $ldapUsername = 'LDAP_USERNAME';
   $ldapPassword = 'LDAP_PASSWORD';
/* CHANGE THESE */

if (!empty($_POST['username']) && !empty($_POST['password'])) {

// filter out ldap wildcards
   if (!preg_match('/^[a-zA-Z0-9\-]+$/', $_POST['username'])) {
       die('Please enter a valid username');
   }

   if (!$ldapConnection = @ldap_connect($ldapServer)) {
       die('Could not connect to ldap server');
   }

   if (!@ldap_bind($ldapConnection, $ldapUsername, $ldapPassword)) {
       die('Could not bind to ldap server');
   }

   if (!$ldapSearch = @ldap_search($ldapConnection, $ldapBaseDomain, 'cn=' . $_POST['username'])) {
       die('Could not complete ldap search');
   }

   $ldapCount = @ldap_count_entries($ldapConnection, $ldapSearch);

   if (!$ldapCount) {
	die('account not found');
} else {
       if (!$ldapEntry = @ldap_get_entries($ldapConnection, $ldapSearch)) {
           die('Could not get ldap entry');
       }

       $distinguishedName = $ldapEntry[0]['distinguishedname'][0];

       if (empty($distinguishedName)) {
           die('Account information not found');
       }

       if(!@ldap_bind($ldapConnection, $distinguishedName, $_POST['password'])) {
           die('Password Incorrect');
       }

       echo '  Logged in successfully
               User Details';

       echo '' . print_r($ldapEntry[0], true) . '';
   }

} else {
   echo '  </pre><form method="post">
           Username: 

           Password: 

           
           </form>';<br>}<br><br>?&g

Posted (edited)

thank you for that it worked, but i could only get it to read from top level ou folders, we have our staff in:

 

towers.school

The Towers School

Staff

and then in two folders

Teaching staff

&

Non teaching staff

 

how can i get it to read both of these Ou's?

 

or read from any of the OU's within staff and further down

Edited by thesk8rjesus
Guest leeneilson
Posted

Glad I could help.

 

The $ldapBaseDomain variable is the base from which you search for users, so adjusting the level will allow you to set where to search from, but it should then search for everything in that

Posted
The $ldapBaseDomain variable is the base from which you search for users, so adjusting the level will allow you to set where to search from, but it should then search for everything in that

 

Okay so i changed the ou="the towers school" but i get the message saying 'account not found' but i know the user is there

 

the towers school>staff>teaching staff>ict>user

Guest leeneilson
Posted

If you remove the @ character from code then error messages will no longer be surpressed, which might give a more meaningful error message.

 

Apart from that I'm not sure what's going on

Posted

i have

 

$ldapBaseDomain = 'ou=The Towers School,dc=towers,dc=school';

 

i can now get it finding out student accounts but recieving 'Account Not Found' when looking for teachers. what i believe that the problem is, is that its not looking far enough down and only going 4 levels

 

Ou>ou>ou>user

 

but we have

 

The Towers School>Staff>Teaching>Dept>user

 

also is there a way that i could get it to look at only whats in the staff ou?

Posted
i have

 

$ldapBaseDomain = 'ou=The Towers School,dc=towers,dc=school';

 

also is there a way that i could get it to look at only whats in the staff ou?

 

$ldapBaseDomain = 'ou=Dept,ou=Teaching,ou=Staff,ou=The Towers School,dc=towers,dc=school';

Posted
$ldapBaseDomain = 'ou=Dept,ou=Teaching,ou=Staff,ou=The Towers School,dc=towers,dc=school';

 

Okay i have done this and i am still recieving the same error message 'Account Not Found' but i that it is there.

Posted

sorted out why it wasn't working it was looking at 'full name' and as we have changed full names it could find the account is there a way that i can get it to look at the usernames possibly a change of this line

 

$distinguishedName = $ldapEntry[0]['distinguishedname'][0];

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...