Get Username from LDAP Authentication - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Get Username from LDAP Authentication

SROSeanerSROSeaner Posts: 17Member
I am writing a website on a NetWare 6.5 server running Apache with PHP enabled. I have set up LDAP authentication so a browser shows a username/password dialog box to get into the site. I wish to capture the username entered that is properly authenticated to the directory service and display it on the webpage in PHP:

Welcome [username here]

Any thoughts would be most helpful

Comments

  • treshrtreshr Posts: 326Member
    I used ldap in this way:
    [code]



    To search for someone from an LDAP server, type in the name of the server:


    type in the search base:

    type in part of their surname:





    [/code]

    And here is some PHP code to access the LDAP server. It uses six functions:

    * ldap_connect and ldap_bind connects and binds to the LDAP server running on a given computer;
    * ldap_search requests the LDAP server to search for entries containing a particular string;
    * ldap_get_entries returns an array containing the entries that were found.
    * ldap_close closes the connection to the LDAP server.

    Here is the code of the ldap.php script:
    [code]


    <?php
    $server = $_POST["server"];
    $search_base = $_POST["search_base"];
    $part_name = $_POST["part_name"];
    $c_result = ldap_connect("$server");
    $b_result = ldap_bind($c_result);
    $s_result = ldap_search($c_result, "$search_base", "cn=*$part_name*");
    $info = ldap_get_entries($c_result, $s_result);
    $numrows = $info["count"];

    if ( $numrows == 0 ) {
    echo "<P>There is no entry with a name of $part_name";
    echo "";
    ldap_close($c_result);
    exit;
    }
    ?>
    <?php
    for ($rownum = 0; $rownum<$numrows; $rownum++) {
    ?>






    <?php
    }
    ?>


    <?php echo $info[$rownum]["cn"][0]; ?>

    <?php echo $info[$rownum]["ou"][0]; ?>

    <?php echo $info[$rownum]["telephonenumber"][0]; ?>

    <?php
    echo "<A HREF=mailto:";
    echo $info[$rownum]["mail"][0];
    echo ">";
    echo $info[$rownum]["mail"][0];
    echo "
    ";
    ?>
    <?php
    ldap_close($c_result);
    ?>


    [/code]

    Hope this helps you a bit.

    --=][tReShR][=--

Sign In or Register to comment.