Environment username to php form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rmiddleton
Forum Newbie
Posts: 1
Joined: Wed Dec 29, 2010 8:15 am

Environment username to php form

Post by rmiddleton »

Having searched for a couple of days I have yet to find the answer to what should be a simple and common issue.
How do I put the "logged in username" into a php form field?
For testing I tried this from the online manual at: http://www.php.net/manual/en/reserved.v ... onment.php

Code: Select all

<?php
echo 'My username is ' .$_ENV["USER"] . '!';
?> 
which displays blank.
However, from a cmd prompt (Windows 7) echo %username% correctly displays the logged in user.
Similarly I would like to capture the IP.
I see these done at many sites, but seem to be unable to find the solution.
Shouldn't I be able to set a PHP variable equal to an Environment variable?
Last edited by Benjamin on Wed Dec 29, 2010 5:04 pm, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Environment username to php form

Post by McInfo »

Try

Code: Select all

getenv('USER')
Be aware that this is the user on the server, not the client.

A likely reason you are unable to access the environment variables through $_ENV is that maybe the variables_order directive in php.ini does not contain an "E". It is recommended that you use getenv() instead.
Post Reply