How to get currently logged on windows username in PHP and Javascript
It is very simple to get the current windows username in PHP. The following one line of code will output the username of the system where the server is running. If you are running this from localhost then your system login name will be shown. But you can not get the visitor’s system login username.
<?php echo getenv("username"); ?>
By using javascript (actually VBscript) you can get the visitor’s windows username. But there are also limitations. It only works on IE.
<script language="VBscript">
Dim X
set X = createobject("WSCRIPT.Network")
dim U
U=x.UserName
MsgBox "username: " & U
</script>
<script language="Javascript">
var a = U;
alert("Hello, " + a.toString());
</script>
This code will show you the current windows username. But won’t work if run from http://. Open the page from your computer with IE and it’ll work, otherwise not.
Actually it is not possible to get the windows username of your website visitors as it is a security issue. So don’t waste your time if you are trying to do that.
Related posts:
- Under Windows 7 laptop reduces more battery life
- Windows phone 7 cannot run applications written for older versions
- Javascript timer – advanced features, simple to use
- Windows Phone 7 Applications have to be allowed by Microsoft
- Simplify your query easily
19 Comments to “How to get currently logged on windows username in PHP and Javascript”
Leave a Reply



My Elance Profile
Hi adnan, finally a bit of sample for my case !!!
In my intranet to avoid double-login … wanted to read the user logged (user@domain.xxx)
The url of application is http://192.168.1.211
<?php echo getenv("username"); ?>… works ?
thanks
thank you @paskuale for sharing the info, but the link you provided does not open.
Ehm … sorry adnan that was an example of IP class, not the direct link to my app !!
127.0.0.1 = localhost
192.168.1.211 = private class IP or not ?
byez
I tried it on my intranet … always shows the same nick –> “Administrator” !!
oh! i see. for your localhost it’ll always be Administrator. if you browse your localhost from another pc via LAN it’ll be the same!
sigh …. I wanted to avoid the classic login since it is an intranet application, users have the same domain, don’t exist a php class or plugin for recognize the user logged on windows ?
byez
@paskuale, No there is no way to do such thing as this is a security issue.
….even an ActiveX ? ok I will use a file to be placed on any PC, similar to the technique
cookies !!
only solution.
thanks
nope, ActiveX won’t work :(
It is possible in ASP.Net
… we are the competition !!!!! :) Php lovers !
thanks djpic
@djpic, No, It’s not possible even in ASP.Net
Buenas Tardes.! si es lo q creo q es..! Muchisimas gracias.!
el nombre de usuario q bota esa variable es el que està logado en windows? ahora bien como ago para tener el usuario de linux?? el código sigue siendo el mismo? saludos.
Gracias @NB :)
Spanish to English translation: http://bit.ly/718AHI
Good Afternoon.! if the q creo q es ..! Thanks so much.!
the user name variable q is the boot that you’re logged on windows? ago however as to have the linux user? the code remains the same? greetings.
mod_auth_sspi can help you get the domain/username of the logged on user. too bad its barely supported at this time and has some bugs.
My friends it is possible to get the username of the user browsing your application using javascript and active X also the domain name. Though this may be a security risk for some, thats why you have to manually configure your users browser to allow the activeX script to run. Just a one time configuration thats it. Been doing this for 3 yrs now on all our intranet applications that ive developed.
Another option id suggest is to use LDAP Authentication since its more secure.
@haringhabagat, I see. May I have a look at your script. Send me a link pls.
Thanks
Adnan
@Adnan and @paskuale Ive tried posting the script here twice but there seems to be a problem when you try posting with special characters since the script im trying to post is javascript in nature. I get forwarded to a blank page and when i navigate back to your blog my post doesnt reflect. You can just email me at joseroyjavelosa@gmail.com and ill reply with the script. Thanks and more power
guys i finally decided to make my own blog :) cheers you can find the script at my blog below
_http://javajave.weblog.com/how-to-get-currently-logged-on-windows-username-and-domain-name-using-javascript/
hope this helps :)