Jump to content

PHP Active Directory Get displayName Field for Current User


djollie029
Go to solution Solved by djollie029,

Recommended Posts

Hi all,

 

I have been trying to find the answer to this for the past 3 days by searching many forums and am resorting to asking the question myself now as I'm totally stuck!

 

Quite simply I need to do the following for a new Staff Intranet I'm building that uses Windows Authentication in IIS...

 

I am a system administrator and so have full access to our Active Directory on Server 2012.

 

I need a simple PHP script that takes the username of the current logged on user $_SERVER["AUTH_USER"] and connects to our local Active Directory (LDAP) for which I can provide administrator credentials for (so it does not need to be an anonymous connection). I then need the script to look up the user based on the Auth_User result above for the user looking at the Intranet Site, and return the DisplayName and Email Address for that user and store them in a session to be used elsewhere on the Intranet Site.

 

In theory I imagine this is quite simple, however I have struggled to get other scripts working to what I need.

 

Any help would be really appreciated!!

 

Thank you :-)

Link to comment
Share on other sites

People are here to help, but not write scripts on demand.  

 

As you have indicated, you already understand that AD is LDAP compliant, so you need to make sure you have enabled LDAP support.  The details and options for his depend on the OS of your webserver running PHP.

 

If you have code, and specific issues, you should post them here, but until people have something to help you with, I'm not sure what anyone can do for you.

Link to comment
Share on other sites

Thanks @Barand for your help.

 

I have also been playing around with adLDAP for a couple of days but cannot quite get the SSO element to work.

 

In the adLDAP I have set useSSO variable to True.
I have then tried multiple ways to call that file and authenticate a user with no luck.

Below I have posted the cutdown code I am trying to test with. What am I doing wrong please?
php_ldap.dll is enabled in the PHP ini file.

Thanks in advance!

<?php
require_once(dirname(FILE) . '/../src/adLDAP.php');
$adldap = new adLDAP();
$adldap->user($username, $password);
$result=$adldap->user()->infoCollection("username", array("*"));
echo $result->displayName;
echo $result->mail;
?>

Link to comment
Share on other sites

  • Solution

I've finally managed to get what I need working with the adLDAP and SSO.

 

For future reference for anyone else looking for similar solution to get the firstname and lastname from Active Directory that matches the logged on users username...

 

<?php
session_start();
$username = explode('\\',$_SERVER["LOGON_USER"]);
$_SESSION['username'] = strtolower($username[1]);
?>

<?php 
include ("/../src/adLDAP.php"); 
$ldap=new adLDAP();
$userinfo = $ldap->user()->info($_SESSION['username'],array("givenname","sn")); 
$_SESSION['firstname'] = $userinfo[0][givenname][0]; 
$_SESSION['lastname'] = $userinfo[0][sn][0];
$firstname = $_SESSION['firstname']; 
$lastname = $_SESSION['lastname'];
?> 
<br />
<strong>Welcome </strong><?php printf("<b><i>$firstname $lastname</i></b>"); ?>
  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.