简体   繁体   中英

Grabbing a user's username after verification, and storing it into a mySQL database

I recently built a form for the business office that faculty fills out, and the data is stored into a mySQL database, which can then be downloaded into an excel spreadsheet. Everything is working fine, except now they want to know who is logged in and filling out this form.

When a user tries to access the web page that the form is on, a box pops up and asks them to enter in their username and password, before the page will load. This functionality was not set up by me, because I work for a university, and I don't have access to the database that this information is in.

I was told to use $_SERVER['REMOTE_USER']; to grab who is logged into the page and then store it into the database. Here is my code:

//create a variable to store the career account username
$contact= $_SERVER['REMOTE_USER'];

//Insert values from the form into the database
$sql="INSERT INTO payroll (dateSubmitted, lname, fname, orgUnit, action, payArea, pernr, contact) VALUES ('$dateSubmitted','$_POST[lname]','$_POST[fname]','$_POST[orgUnit]','$_POST[action]','$_POST[payArea]','$_POST[pernr]','$contact')";

The problem is, nothing is getting stored into the database under "contact". I even tried setting $_SERVER['REMOTE_USER']; to a string, but it's not saving it.

What am I missing or doing wrong? Any help would be appreciated! Thank you!!

$_SERVER['REMOTE_USER'] doesn't exist in the PHP definition, which is what your problem is. If you turn on E_NOTICE you will likely get a warning about 'REMOTE_USER' not being defined as an index of $_SERVER

Some other options:

$_SERVER['REMOTE_ADDR'] gives the IP of the user connecting (example: 34.66.21.183) $_SERVER['REMOTE_HOST'] gives the reverse dns look up of the REMOTE_ADDR value (example: www.google.com)

This is not however what you want. You need to either save the username in $_SESSION (which requires starting a session and reloading it on each page that is relevant) or use $_COOKIE which allows you to drop small bits of information for a duration of time.

If you are working with a CMS (Content Management System) You might have other options to get the user variable.

尝试$_SERVER['HTTP_AUTH_USER']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM