469,270 Members | 1,092 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

getting mac address of client machines

pradeepjain
563 512MB
Hii,
I need to know is there any way to get the mac id's of the machines accessing ma site! I want to make a access lock using the mac id's
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function returnmacaddress() {
  3. // This code is under the GNU Public Licence
  4. // Written by michael_stankiewicz {don't spam} at yahoo {no spam} dot com
  5. // Tested only on linux, please report bugs
  6.  
  7. // WARNING: the commands 'which' and 'arp' should be executable
  8. // by the apache user; on most linux boxes the default configuration
  9. // should work fine
  10.  
  11. // get the arp executable path
  12. $location = 'which arp';
  13. $location = rtrim($location);
  14. // Execute the arp command and store the output in $arpTable
  15. $arpTable = '$location -n';
  16. // Split the output so every line is an entry of the $arpSplitted array
  17. $arpSplitted = split("\n",$arpTable);
  18. // get the remote ip address (the ip address of the client, the browser)
  19. $remoteIp = $_SERVER['REMOTE_ADDR'];
  20. $remoteIp = str_replace(".", "\\.", $remoteIp);
  21. // Cicle the array to find the match with the remote ip address
  22. foreach ($arpSplitted as $value) {
  23. // Split every arp line, this is done in case the format of the arp
  24. // command output is a bit different than expected
  25. $valueSplitted = split(" ",$value);
  26. foreach ($valueSplitted as $spLine) {
  27. if (preg_match("/$remoteIp/",$spLine)) {
  28. $ipFound = true;
  29. }
  30. // The ip address has been found, now rescan all the string
  31. // to get the mac address
  32. if ($ipFound) {
  33. // Rescan all the string, in case the mac address, in the string
  34. // returned by arp, comes before the ip address
  35. // (you know, Murphy's laws)
  36. reset($valueSplitted);
  37. foreach ($valueSplitted as $spLine) {
  38. if (preg_match("/[0-9a-f][0-9a-f][:-]".
  39. "[0-9a-f][0-9a-f][:-]".
  40. "[0-9a-f][0-9a-f][:-]".
  41. "[0-9a-f][0-9a-f][:-]".
  42. "[0-9a-f][0-9a-f][:-]".
  43. "[0-9a-f][0-9a-f]/i",$spLine)) {
  44. return $spLine;
  45. }
  46. }
  47. }
  48. $ipFound = false;
  49. }
  50. }
  51. return false;
  52. }
  53. echo returnmacaddress();
  54. ?>
I found this code by googling! But its not printing anything!! :(
Sep 10 '09 #1

✓ answered by Dormilich

why should the system ID / CPU ID be submitted? you can’t read what’s not provided.

if those computers have static IPs, you can use those.

29 36813
Dormilich
8,651 Expert Mod 8TB
then itís probably returning false, which does not have a print representation (check with var_dump()).
Sep 10 '09 #2
pradeepjain
563 512MB
@Dormilich
I did not get u!! :( !i have never used var_dump()!!
Sep 10 '09 #3
Dormilich
8,651 Expert Mod 8TB
@pradeepjain
shame on you, thatís probably the #1 debugging function in PHP. see ref.
Sep 10 '09 #4
pradeepjain
563 512MB
@Dormilich
okie!!i will learn now!!thanks for the comments !!
Sep 10 '09 #5
pradeepjain
563 512MB
I tried to print the $location value!!! it had nothin in it!!!
Sep 10 '09 #6
Dormilich
8,651 Expert Mod 8TB
@pradeepjain
where did you try to print it?
Sep 10 '09 #7
pradeepjain
563 512MB
after this line

Expand|Select|Wrap|Line Numbers
  1. $location = rtrim($location);
Sep 10 '09 #8
Dormilich
8,651 Expert Mod 8TB
may I see the code?

my output:
Expand|Select|Wrap|Line Numbers
  1. // formatted through xdebug
  2. string 'which arp' (length=9)
Sep 10 '09 #9
pradeepjain
563 512MB
@Dormilich
sorry for the mistake i am also getting 'which arp' ...
i dono y he he is using 'which arp ' here! i am not getting the logic in that code! so i am facing problem!
Sep 10 '09 #10
Dormilich
8,651 Expert Mod 8TB
hasnít there been a description?

besides the code usage looks odd.

this is what I found somewhere else and looks way more sensible.
Expand|Select|Wrap|Line Numbers
  1. $mac = `ping $ip && arp -a | grep $ip`;
Sep 10 '09 #11
pradeepjain
563 512MB
@Dormilich
okie thanks for help!will give it a try!
Sep 10 '09 #12
pradeepjain
563 512MB
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $ip = '192.168.2.213';
  3.  
  4.   $mac = `ping $ip && arp -a | grep $ip`;
  5. echo $mac;
  6. ?>
i wrote this code !!its printing nothing! i am getting an error in error log like

ping: icmp open socket: Permission denied when i run this code!
Sep 10 '09 #13
Dormilich
8,651 Expert Mod 8TB
to know what arp does go to a posix machine (linux, unix, bsd, mac), open a terminal (on linux you can also use ALT+F2), type
Expand|Select|Wrap|Line Numbers
  1. man arp
hit enter and read the description.
Sep 10 '09 #14
Dormilich
8,651 Expert Mod 8TB
@pradeepjain
thatís always the problem with shell commands, you need permission.
Sep 10 '09 #15
pradeepjain
563 512MB
so we cannot generally find the MAC id's of a machine connected to net as one of the problems will be permission!
Sep 10 '09 #16
Dormilich
8,651 Expert Mod 8TB
do you get the same result if you try
Expand|Select|Wrap|Line Numbers
  1. $mac = `arp -a | grep $ip`;
?
Sep 10 '09 #17
pradeepjain
563 512MB
@Dormilich
yeah!!!permission error!
Sep 10 '09 #18
Dormilich
8,651 Expert Mod 8TB
then you canít do anything about it (although the concept is right)
Sep 10 '09 #19
pradeepjain
563 512MB
is there any parameter by which we can recognize a system over internet ?
Jan 11 '10 #20
Dormilich
8,651 Expert Mod 8TB
recognize in what kind of way?
Jan 11 '10 #21
pradeepjain
563 512MB
like suppose if that system is accessing ma website its should be identified by unique thing specific to that comp! bcos we need to lock our website for a few ips' of outside internet!
Jan 11 '10 #22
Dormilich
8,651 Expert Mod 8TB
what is a "comp", company, computer, competitor?

do you mean allow certain IPs or exclude certain IPs?
Jan 11 '10 #23
pradeepjain
563 512MB
sorry for shortcuts..i mean to say a computer /pc
Jan 11 '10 #24
Dormilich
8,651 Expert Mod 8TB
is there anything specific about allowed/denied computers/clients?
Jan 11 '10 #25
pradeepjain
563 512MB
its like the website will be accessed at only 2 places say 2 hospitals so need to lock them to those places!
Jan 11 '10 #26
pradeepjain
563 512MB
IS there no solution for this problem . like i am using all linux machines . cant i uniquely identify the system accessing it .something like system ID / CPU ID.
Feb 2 '10 #27
Dormilich
8,651 Expert Mod 8TB
why should the system ID / CPU ID be submitted? you can’t read what’s not provided.

if those computers have static IPs, you can use those.
Feb 2 '10 #28
pradeepjain
563 512MB
nope !! but they use laptops for accessing the site. so they dont have a static IP's .
Feb 2 '10 #29
This functionality will work on a local network, but not the internet as the MAC is hidden by the clients router.

In short, the MAC is not present in the IP packet header.

You can retrieve the client IP address via <?php echo $_SERVER['REMOTE_ADDR']; ?> however this is not 100% accurate because of IP address spoofing.
Oct 15 '10 #30

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Marchello | last post: by
7 posts views Thread by navin123 | last post: by
2 posts views Thread by =?Utf-8?B?S2plbGw=?= | last post: by
5 posts views Thread by =?Utf-8?B?ZGF2aWQ=?= | last post: by
3 posts views Thread by =?Utf-8?B?R2xlbm4gVGhpbW1lcw==?= | last post: by
omerbutt
5 posts views Thread by omerbutt | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.