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:

  1. Under Windows 7 laptop reduces more battery life
  2. Windows phone 7 cannot run applications written for older versions
  3. Javascript timer – advanced features, simple to use
  4. Windows Phone 7 Applications have to be allowed by Microsoft
  5. Simplify your query easily

19 Comments to “How to get currently logged on windows username in PHP and Javascript”

  1. paskuale 23 November 2009 at 4:51 am #

    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

  2. adnan 23 November 2009 at 12:00 pm #

    thank you @paskuale for sharing the info, but the link you provided does not open.

  3. paskuale 23 November 2009 at 1:48 pm #

    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

  4. paskuale 23 November 2009 at 2:01 pm #

    I tried it on my intranet … always shows the same nick –> “Administrator” !!

  5. adnan 23 November 2009 at 2:54 pm #

    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!

  6. paskuale 23 November 2009 at 3:19 pm #

    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

  7. adnan 23 November 2009 at 3:53 pm #

    @paskuale, No there is no way to do such thing as this is a security issue.

  8. paskuale 23 November 2009 at 5:12 pm #

    ….even an ActiveX ? ok I will use a file to be placed on any PC, similar to the technique
    cookies !!

    only solution.

    thanks

  9. adnan 24 November 2009 at 1:09 am #

    nope, ActiveX won’t work :(

  10. djpic 16 December 2009 at 8:05 pm #

    It is possible in ASP.Net

  11. paskuale 16 December 2009 at 8:16 pm #

    … we are the competition !!!!! :) Php lovers !

    thanks djpic

  12. adnan 16 December 2009 at 10:30 pm #

    @djpic, No, It’s not possible even in ASP.Net

  13. NB 21 December 2009 at 2:53 am #

    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.

  14. adnan 21 December 2009 at 10:56 am #

    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.

  15. Zenshai 12 February 2010 at 9:43 pm #

    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.

  16. haringhabagat 7 June 2010 at 8:18 am #

    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.

  17. ABM Adnan 7 June 2010 at 8:44 am #

    @haringhabagat, I see. May I have a look at your script. Send me a link pls.

    Thanks
    Adnan

  18. haringhabagat 7 June 2010 at 9:07 am #

    @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

  19. haringhabagat 17 June 2010 at 3:24 pm #

    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 :)


Leave a Reply