Image Image Image Image Image

© Copyright 2012 ABCoder | Email | RSS

Scroll to Top

To Top

Core JavaScript tips and tricks: playing with javascript

How to get currently logged on windows username in PHP and Javascript

On 27, Dec 2008 | 29 Comments | In Core JavaScript, Database, JavaScript, MySQL, PHP | By abcoder

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.

Tags | , , , , , , , , , , , , , ,

Comments

  1. 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. thank you @paskuale for sharing the info, but the link you provided does not open.

  3. 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. I tried it on my intranet … always shows the same nick –> “Administrator” !!

  5. 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. 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. @paskuale, No there is no way to do such thing as this is a security issue.

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

    only solution.

    thanks

  9. nope, ActiveX won’t work :(

  10. It is possible in ASP.Net

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

    thanks djpic

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

  13. NB

    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. 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

    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

    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. @haringhabagat, I see. May I have a look at your script. Send me a link pls.

    Thanks
    Adnan

  18. haringhabagat

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

  20. can anyone here able to see the javascript in the last post by haringhabagat?

    _http://javajave.weblog.com/how-to-get-currently-logged-on-windows-username-and-domain-name-using-javascript/

  21. NO

  22. Flipzen

    The vbscript example works fine, but only in Internet Explorer and only on a webserver, that it is declared as a trusted server.

    PHP is a serverside script so the PHP getenv(“username”) returns the userid that runs the website & PHP service. Not the browsing user.

  23. Flipzen

    set X = createobject(“WSCRIPT.Network”)
    document.write(x.UserName)

  24. Noman

    hi,
    is this possible i get the domain of Loged in user.i mean when some one on our network visit the jsp page i check on which domain this user is loged in

  25. I am not sure whether getenv() will provide system logon name(if IIS is not your web server) .

    I will recommend using mod_ntlm an apache module to get windows logon name. Well you have to make some configuration on http.conf file.

  26. Thank you for such a fantastic blog. Where else could anyone get that kind of info written in such a perfect way? I have a presentation that I am presently working on, and I have been on the look out for such information

    Regards,

  27. Hi there..
    actually i wanna know about how to create session when the user logged in didnt got the answer from the above talk can any one please??

  28. Hossain

    How to use the script in a php code, can anyone help me…??

  29. Throughout the great design of things you’ll secure an A for effort. Exactly where you actually confused everybody ended up being on your particulars. You know, as the maxim goes, details make or break the argument.. And it could not be more accurate in this article. Having said that, let me reveal to you just what did deliver the results. Your text is actually highly engaging which is possibly why I am making the effort to comment. I do not really make it a regular habit of doing that. Next, while I can notice the jumps in logic you make, I am not really confident of just how you seem to connect your ideas which inturn produce the actual final result. For right now I shall yield to your point but wish in the future you link your dots better.

Submit a Comment