PHP

Moderators: None (Apply to moderate this forum)
Number of threads: 1845
Number of posts: 5013

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Get Username from LDAP Authentication Posted by SROSeaner on 27 Mar 2005 at 8:29 PM
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
Report
Re: Get Username from LDAP Authentication Posted by treshr on 29 Mar 2005 at 1:16 AM
I used ldap in this way:
<HTML>
<BODY> 
<FORM METHOD="POST" ACTION="ldap.php"> 
To search for someone from an LDAP server, type in the name of the server:<BR>
  <INPUT TYPE="text" NAME="server">
  type in the search base:
  <INPUT TYPE="text" NAME="search_base">
   type in part of their surname:
  <INPUT TYPE="text" NAME="part_name"> 
  <INPUT TYPE="submit" VALUE="Run query"> 
</FORM> 
</BODY>
</HTML> 


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:
<HTML>
<BODY>
<?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</P>";
    echo "</BODY></HTML>";
    ldap_close($c_result);
    exit;
  }
?> 
<TABLE BORDER="1">
<?php
  for ($rownum = 0; $rownum<$numrows; $rownum++) {
?>
<TR>
  <TD>
<?php echo $info[$rownum]["cn"][0]; ?>
  </TD>
  <TD>
<?php echo $info[$rownum]["ou"][0]; ?>
  </TD>
  <TD>
<?php echo $info[$rownum]["telephonenumber"][0]; ?>
  </TD>
  <TD> 
<?php 
  echo "<A HREF=mailto:"; 
  echo $info[$rownum]["mail"][0];
  echo ">";
  echo $info[$rownum]["mail"][0];
  echo "</A><BR>";
?>
  </TD>
</TR>
<?php 
  } 
?> 

</TABLE>
<?php
   ldap_close($c_result);
?>
</BODY>
</HTML>


Hope this helps you a bit.

--=][tReShR][=--




 

Recent Jobs