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.
-
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
-
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
-
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/
-
NO
-
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.
-
set X = createobject(“WSCRIPT.Network”)
document.write(x.UserName) -
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 -
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.
-
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,
-
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?? -
How to use the script in a php code, can anyone help me…??
-
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
© Copyright 2012 ABCoder |













Comments