#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    1
    Rep Power
    0

    Ldap_connect error handling


    Hello all -

    I am coding a script to authenticate a user's username/password against active directory using the php ldap functions.

    When using ldap_connect, if I provide an invalid LDAP server the error does not get thrown until the subsequent ldap_bind command is encountered. For instance here's one approach:

    $adserver = "ldap://XXadmin.metropolitan";
    $ds=ldap_connect($adserver)
    or die("Could not connect to host $adserver");

    The server is not valid but the "or die" action is not executed. Here is another approach:

    $adserver = "ldap://XXadmin.metropolitan";
    $ds=ldap_connect($adserver);
    if ($ds) {
    ...
    ...
    } else {
    echo "Unable to connect to LDAP server";
    }

    In this case the else commands are not executed even thought the LDAP server is invalid.

    In both cases the ldap_bind function produces the error: 'Can't contact LDAP server', the bind fails and the script halts.

    I'd like to have the ldap_connect function trip the error instead of the ldap_bind function.

    How have others in the forum handled this?

    Thanks,
    PJP
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    According to the manual:
    Originally Posted by PHP Manual
    When OpenLDAP 2.x.x is used, ldap_connect() will always return a resource as it does not actually connect but just initializes the connecting parameters. The actual connect happens with the next calls to ldap_* funcs, usually with ldap_bind().
    Do your error handling on the second function call, or use a different LDAP library.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo