Reputation: 7163
I know that we can get the MAC address of a user via IE (ActiveX objects).
Is there a way to obtain a user's MAC address using JavaScript?
Upvotes: 125
Views: 279656
Reputation: 35
I know I am really late to this party. And although the answer is still no. I found a way to generate and store a unique id that helps to keep track of a user while they navigate the site.
When the user signs up, I then have a full record of what pages he visited before he signed up. I also store this id in the user table for historical reference.
This is also handy when you're looking for dubious activity. For example, a user that has created multiple accounts. I should note that this is on a financial transaction site and the data is only used internally. It does really help to cut down on fraudulent and duped accounts. There is a lot that can be done with localStorage and this method, but I will keep this short to not give anyone nefarious ideas.
1- Generate a random string. If you generate a 40 char string, you don't really have too much to worry about as far as them colliding. We're not looking to send a rocket to Mars here.
I use a small PHP function to generate a 40 char string. I use Ajax to call this function and retrieve the result.
function genrandstr($length=NULL) {
if($length == NULL){ $length = 30; }
$characters =
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
localStorage.setItem('id', id) to add to localStorage
(let) or (var) id = localStorage.getItem('id') to read from localStorage
2- Using cookies, you can store this id in a cookie.
3- LocalStorage is your friend. Store the same id in LocalStorage and chances are, you will always have that id to look at.
With an id stored in LocalStorage, your user can delete cookies and you'd still be able to recognize an old visitor by this id.
If the user deletes all their data, then you're SOL and start all over again when they revisit.
Have Fun
Upvotes: 1
Reputation: 1
No you can't obtain a user's MAC address using JavaScript in another way, just by using active X op for Microsoft in IE browser
Upvotes: -3
Reputation: 579
i was looking for the same problem and stumbled upon the following code.
How to get Client MAC address(Web):
To get the client MAC address only way we can rely on JavaScript and Active X control of Microsoft.It is only work in IE if Active X enable for IE. As the ActiveXObject is not available with the Firefox, its not working with the firefox and is working fine in IE.
This script is for IE only:
function showMacAddress() {
var obj = new ActiveXObject("WbemScripting.SWbemLocator");
var s = obj.ConnectServer(".");
var properties = s.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration");
var e = new Enumerator(properties);
var output;
output = '<table border="0" cellPadding="5px" cellSpacing="1px" bgColor="#CCCCCC">';
output = output + '<tr bgColor="#EAEAEA"><td>Caption</td><td>MACAddress</td></tr>';
while (!e.atEnd()) {
e.moveNext();
var p = e.item();
if (!p) continue;
output = output + '<tr bgColor="#FFFFFF">';
output = output + '<td>' + p.Caption; +'</td>';
output = output + '<td>' + p.MACAddress + '</td>';
output = output + '</tr>';
}
output = output + '</table>';
document.getElementById("box").innerHTML = output;
}
showMacAddress();
<div id='box'></div>
Upvotes: 5
Reputation: 13118
I concur with all the previous answers that it would be a privacy/security vulnerability if you would be able to do this directly from Javascript. There are two things I can think of:
Upvotes: 74
Reputation: 36120
No you cannot get the MAC address in JavaScript, mainly because the MAC address uniquely identifies the running computer so it would be a security vulnerability.
Now if all you need is a unique identifier, I suggest you create one yourself using some cryptographic algorithm and store it in a cookie.
If you really need to know the MAC address of the computer AND you are developing for internal applications, then I suggest you use an external component to do that: ActiveX for IE, XPCOM for Firefox (installed as an extension).
Upvotes: 42
Reputation: 70243
Nope. The reason ActiveX can do it is because ActiveX is a little application that runs on the client's machine.
I would imagine access to such information via JavaScript would be a security vulnerability.
Upvotes: 7
Reputation: 7934
If this is for an intranet application and all of the clients use DHCP, you can query the DHCP server for the MAC address for a given IP address.
Upvotes: 7
Reputation: 75869
The quick and simple answer is No.
Javascript is quite a high level language and does not have access to this sort of information.
Upvotes: 70