Link to home
Start Free TrialLog in
Avatar of Bernie Salvaggio
Bernie SalvaggioFlag for United States of America

asked on

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.

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of hernst42
hernst42
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of minichicken
minichicken

Hi

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: http://www.phpfreaks.com/tutorials/41/0.php


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.

<?
session_start();
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";
             }
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
</Directory>
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...

_Blue
Avatar of Bernie Salvaggio

ASKER

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 http://httpd.apache.org/docs-2.0/mod/mod_auth_ldap.html
http://mgmt.uanet.ua.ac.be/u/dbruyne/mod_auth_ldap_apache2.html

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 ?
http://www.firepages.org/public/mod_auth_sspi-apache2046.zip
hernst42, awesome! Thank you so much, that worked.  

I downloaded the zip you just posted, copied mod_auth_sspi.so to the modules directory, included the line:
LoadModule sspi_auth_module modules/mod_auth_sspi.so
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
</Directory>

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?

Thanks!
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