Retrieve logged in username

I'm using PHP4 on Apache2 on W2K server, we're running AD but the server this is running on isn't a DC.  I'm trying to make a drop box for students to submit files to teachers.  However, I don't want students submitting work for eachother.

I'm thinking that the easiest way would be to just retrieve the username of the current logged on user but I don't know if that is possible.  It would also have to work with 2k, XP, and 98 clients.

Bernie SalvaggioIT ManagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If you are using the IE or Mozilla >= 1.4 as webbrowser use the NTLM protocol to authentificate the user on the remote side.

As starting links have a look at:

Don't know if NTLM is available on WIn98, but I think so.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

If you would like to retrieve the username of the logged in user. You will need to create a session for the user.
Info on sessions:

Basically, when the user enters his username and password and submits the details for access check, if the username and password is correct then you assign the user a session variable like $_SESSION['username']  = "Student Name".  

The session variable become available across the site, so on the submit work page, you can retrieve the students username without him re-entering his username.

Note about session in PHP: REmember to have the following at the top top of your page in order to use session in that particular page. When I mean top of the page, I mean before any output or HTML or even a blank space.

header("Cache-Control: private"); //IE 6 fix
Hmmm. If you can get the password file for the students who login, you could use either .htaccess or the apache.conf to configure the upload page so that the students need to login to your site before they can upload. Then:

             if(isset($_SERVER['AUTH_TYPE'])) {
                        print "User: " . $_SERVER['PHP_AUTH_USER'];

             } else {
                        print "Nobody Logged in";
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Example for your http.conf (NOT apache.conf !!!)

<Directory '/private/utilities'>      
      Options -Indexes
      Order Deny,Allow
      Deny from All
      AuthName "Confirm Your Login"
      AuthType Basic
      AuthUserFile /private/passwords.pwd
      Require valid-user
Please note that for security reasons you can't get the current Windows login username across the Internet Zone. You therefore have to either

1) Get the students to log into the server as well as the computer, the solutions above all address some aspect of that.
2) Make an ActiveX control which can be used on your Intranet to pass the Windows login name to the server.

The approach you want to take determines the help we can give you...

Bernie SalvaggioIT ManagerAuthor Commented:
I guess I'll have to go with a solution where the student logs into a PHP session.

hernst42, I couldn't get that to work, I think I'm missing a step, I don't know.  It seems specific to Tiki and I'm not using that.

It's looking like I'll go with the combo of setting up a PHP session as suggested by minichicken and authenticating to a .htpasswd file as basiclife suggested.

However, I have 950 students.  There's no way I'm manually typing out that many usernames and passwords.  Not to mention that I don't know their passwords.  Any suggestions as to getting their passwords from AD into a file, into a .htpasswd file? I suppose I could set them all up with a default password if I can't get the passwords out of AD, but then I need information on how to let a user change their password that's stored in a .htpasswd file...

Any thoughts?
Depending on how the passwords are stored in AD, you might eb able to point your .htaccess file straight to that password file. I'm not sure how it encrypts passwords so I can't guarantee success. As to changing the password, you need to use the command "htpasswd"

I'm running Apache on a windows box so you'll need to modify this slightly but...

$ret = shell_exec("C:\path\to\htpasswd\htpasswd.exe -b d:\path\to\password\list $username $password");

should allow you to change the password using PHP. Of course this deopends on the script having permissions for the files etc...
As a suggestion: Does your site have an authenticated login service for any other purpose? IE accessing student-only documents from off-site etc... ? If so, it's probably be easier to add your upload site to that security and elt the existing system do the authentication, then grab the logged in user using the PHP code above. This would save you keeping a parallel password list and also mean you don't have to woryy about authentication at all.
Bah! More thoughts as they come to me: If you DO keep your own password list and someone changes their password, they'll be required to login again as soon as they try to go to another page (as the browser will send the old credentials) you'll probably want to mention that on the page as soon as it has changed their password. Also, you'll want to test the value of $ret to make sure the password change has been successful. Finally, if there are any hiccups, the output from htpasswd will be piped to the Apache error log.
As you have AD you can use either the ldap-methods provided by php to verify the user or do this with mod_auth_ldap of apache2. See

If you setup apache to auth the user you can access the username of the authenticated user in PHP by $_SERVER['REMOTE_USER']

So the passwords are in sync and no maintainance need to be done. In this case you also should use a SSL-connection as those username and password would be send in plaintext to the webserver when using http-requests.
What was the problem with the mod_auth_sspi, because I might be trying by myself in the future. Have you tried these precompiled so for apache2 ?
Bernie SalvaggioIT ManagerAuthor Commented:
hernst42, awesome! Thank you so much, that worked.  

I downloaded the zip you just posted, copied to the modules directory, included the line:
LoadModule sspi_auth_module modules/
in the httpd.conf as well as:

<Directory "C:/Program Files/Apache Group/Apache2/htdocs/dropbox">
    AllowOverride None
    Options None
    Order allow,deny
    Allow from all
    AuthName "Login using your DOMAIN username and password"
    AuthType SSPI
    SSPIAuth On
    SSPIAuthoritative On
    SSPIOfferBasic On
    require valid-user

Then I used, as you said, $_SERVER['REMOTE_USER'] to access the username.  So it was kind of a combo of your 3 posts that gave the answer.  I'll close this Q and assign you points in just a bit...  Before that, for some extra points, say, 50-100, I don't suppose there's a way to grab the user's actual name, not just the username, from AD as well? Perhaps using the same $_SERVER variable?

Bernie SalvaggioIT ManagerAuthor Commented:
I've been trying to use the mod_auth_ldap to do what I just mentioned but when I add the line LoadModule auth_ldap_module modules/mod_auth_ldap.dll to httpd.conf it fails to start.  Same thing happens when I try to use the .so version of mod_auth_ldap...
You'd have to query the AD yourself to get student name, but there should be a simple command-line way to do that and parse the response for the real name / any other details you want
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.