Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: PHP and LDAP

  1. #1
    New Coder
    Join Date
    Sep 2010
    Posts
    35
    Thanks
    3
    Thanked 1 Time in 1 Post

    PHP and LDAP

    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
    Code:
    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
    Code:
    $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:
    Code:
    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.
    Last edited by Mark91; 11-19-2010 at 01:30 AM.

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    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);

  • Users who have thanked MattF for this post:

    Mark91 (11-19-2010)

  • #3
    New Coder
    Join Date
    Sep 2010
    Posts
    35
    Thanks
    3
    Thanked 1 Time in 1 Post
    Silly me... Thanks mate, that worked perfectly. I can now use $initConn through all of my functions within the LDAP class.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •