...

View Full Version : PHP and LDAP



Mark91
11-18-2010, 11:03 PM
Hey guys. I'm currently developing a small system which integrates with an LDAP directory for user authentication. I am having troubles converting the code into a more OOP approach.

At the moment, this is the code I have... which is very messy. This code DOES work but I am looking to convert it into classes to assist with my OOP learning.

config.php

define("LDAP_USER", "uid=xxx,ou=xxx,dc=xxx,dc=xxx,dc=xxx");
define("LDAP_PASS", "xxxxx");
define("BASE_DN", "dc=xxx,dc=xxx,dc=xxx");

$ldapConnect = ldap_connect("127.0.0.1") or die("Error: Could not connect to the LDAP server!");

ldap_set_option($ldapConnect, LDAP_OPT_PROTOCOL_VERSION, 3);

if($ldapConnect){
$ldapbind = ldap_bind($ldapConnect, LDAP_USER, LDAP_PASS);
}
login.php

$filter = "(uid=$username)";

$search = ldap_search($ldapConnect, BASE_DN, $filter, array("uid"));
$getuser = ldap_get_entries($ldapConnect, $search);

if($getuser){
$binduser = @ldap_bind($ldapConnect, $getuser[0]['dn'], $password);
if(!$binduser){
echo "Login failed.";
} else {

$reconnect = ldap_bind($ldapConnect, LDAP_USER, LDAP_PASS);
if($reconnect){
$groupdn = "cn=xxx,ou=xxx,ou=xxx,dc=xxx,dc=xxx,dc=xxx";
$compare = ldap_compare($ldapConnect, $groupdn, 'member', $getuser[0]['dn']);

if($compare){
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
setcookie(cookie1, $username, 0);
setcookie(cookie2, md5($password), 0);
header("location: index.php");
} else {
return false;
}
}
}
} else {
echo "Login failed.";
}

This is what I've got so far in the config.php file:

class LDAP {

protected $ldap_host = "xxxxx";
protected $ldap_user = "uid=xxx,ou=xxx,dc=xxx,dc=xxx,dc=xxx";
protected $ldap_pass = "xxxxx";
protected $base_dn = "dc=xxx,dc=xxx,dc=xxx";
protected $filter = '(uid=$username)';

public function connect()
{
if(!$this->connection)
{
global $initConn;

$initConn = @ldap_connect($this->ldap_host);
ldap_set_option($initConn, LDAP_OPT_PROTOCOL_VERSION, 3);
if($initConn)
{
$initBind = ldap_bind($initConn, $this->ldap_user, $this->ldap_pass);
$this->connection = true;
return true;
} else {
return false;
}
} else {
return true;
}
}

public function search()
{
$this->search = ldap_search($initConn, $this->base_dn, $filter, array("uid"));
}

}

I am having troubles making $initConn a global variable which I can use throughout the classes.

*Note: xxx was used to blank out details.

MattF
11-19-2010, 02:06 AM
class LDAP {
private $initConn = '';
protected $ldap_host = "xxxxx";
protected $ldap_user = "uid=xxx,ou=xxx,dc=xxx,dc=xxx,dc=xxx";
protected $ldap_pass = "hna1CubiNUZsVhOePz8B";
protected $base_dn = "dc=xxx,dc=xxx,dc=xxx";
protected $filter = '(uid=$username)';

public function connect()
{
if(!$this->connection)
{
$this->initConn = @ldap_connect($this->ldap_host);

Mark91
11-19-2010, 02:34 AM
Silly me... Thanks mate, that worked perfectly. I can now use $initConn through all of my functions within the LDAP class.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum