Retrieve IP and MAC using a PHP script

Thomas -  
 plefebv -
Hello everyone,

As part of my thesis, I need to create a PHP page that depends on a MySQL database.
This page allows a client to log in.
Which allows sending the identifier, the IP, and the MAC to the database.

My question: I am looking for a PHP script to collect the user's IP and MAC to send it to the database.

Thank you
Configuration: athlon 64 300+, gforce 7900gt, 1.5 gb ram, 500 gb sata, windows xp, ubuntu

18 réponses

wAx Posted messages 1596 Registration date   Status Membre Last intervention   202
 
Hello,

It is important to know that it is impossible to know the MAC address of the machine connecting as soon as there is any router between the machine and the server.

MAC addresses are used to pass information from router to router, from the client workstation to the server and vice versa when the server responds.

This means that the MAC address of the connecting machine is only visible to the first router (often even the 'box' like Freebox, Livebox, etc.); then, the second intermediary sees the data arriving from the MAC address of the box, etc.
Thus, the server hosting the site sees ALL connections arriving with the MAC address of its router (of course, unless the request comes from the same network as the server itself).

Retrieving MAC addresses is therefore useless and almost always impossible.

See you soon

wAx
35
Ssylvainsab Posted messages 2892 Status Modérateur 825
 
Hi.

For the IP, it's the variable:
$_SERVER['REMOTE_ADDR']

for example :
<?php //display the IP address echo $_SERVER['REMOTE_ADDR']; ?>


As for the MAC address, I'm not sure, but I believe it's based on the components (ethernet card, wi-fi...) and not the computer.

https://www.php.net/manual/en/reserved.variables.php

see you later
--
Sylvain
12
neo673
 
wAx is absolutely right, it is impossible to know the MAC address of the machine from a php script.

If you tinker a bit by creating a mini software that you send with each client connection like a cookie but which runs on the client to ask for their MAC address and send it back to your server, then you can have fun storing it in a sql database and testing it with each user connection...

In other words, on the one hand it will be very uncomfortable for the user, on the other hand, a kind of script that runs on the client, really not great, and besides, good luck programming all that :-D
8
omniarchos
 
Hello everyone,

reading this topic made me wonder about a question whose answer might allow us to get the client's MAC address (the last post is indeed old but it could still be useful for others).

JavaScript (if I followed the various tutorials I read correctly ^^) runs on the client PC...can a JS script, therefore, retrieve the MAC address (there's no "routing" issue since it's on the PC itself) and send it back to a variable? We might be able to access it, right?
3
pmag
 
<?php
ob_start();
system("ipconfig /all");
$mycom=ob_get_contents();
ob_clean();
$findme = "physique";
$pmac = strpos($mycom, $findme);
$mac=substr($mycom,($pmac+36),17);
echo $mac;
?>

it's giving me 15-9E-36-F6-06

isn't that it???
3
SupKawai
 
It's the server's MAC, however, it's always impossible in PHP to retrieve the client's!
0
wAx
 
Always impossible and it won't change!
0
Duncan Idaho
 
Thank you SupKawai, that's exactly what I was looking for! (the MAC address of the server)
0
jules
 
If the clients connecting are on the same LAN, we can write a small PHP script that executes a system command.

$result=system("arp -a");

will provide all the MAC addresses associated with the IPs that have just connected to the server... then a string processing will allow us to find the MAC of a specific IP.

But this would only work on a LAN. MAC addresses have a local significance.
2
Anonymous user
 
The MAC address is written on the network card (whether it is integrated or not on the motherboard)
--
"The end of the world is not tomorrow, it arrived yesterday" (Lapinkiller)
1
Thomas
 
I know the theory of the ISO protocol, but that’s not what I’m asking. Anyway, thanks anyway

If someone has an idea, still here.
1
wAx Posted messages 1596 Registration date   Status Membre Last intervention   202
 
Good evening,

In that case, knowing the theory of proto Ethernet and the ISO model, you should already know that if your client is not on the same physical network, it is impossible to have the MAC address.

Moreover, this is information that does not reach layer 7, where you work. The IP is part of the environment variables of the web server and is therefore accessible to PHP through various means. The MAC is not accessible...

Good luck.
0
mmlpipf
 
Hello..WAX. You know it's true from a functional point of view, but there can be nuances in my opinion. If this is the case for MAC addresses, I would say it applies to IP addresses as well, because before leaving the network, the local machine's address is mapped to a public address and a port number (the NAT protocol), and so the local IP address is not visible on the web, and the same problem arises there. I think it is possible to know the MAC address, or this functionality is simply not taken into account.
What do you think?
Thank you!!!
0
bud35000 Posted messages 155 Status Membre 103
 
Alright, but if it's on the same network, we can retrieve this MAC address with arp -a and that other command like nbtstat -a "@ip".
But I would also like to know if there's a way to automate this task to record the MAC addresses in a SQL table, matching them with their fixed IP.
So either in PHP or something else, but I imagine there is a query that exists.

With a batch script, we can visualize the corresponding MAC addresses from the IPs. Is it possible to retrieve these values as variables?
1
tchicanos
 
Hello, can someone help me find a software or a program in PHP, shell, Perl, or even Cygwin that would allow me to find a MAC address on a switch port? This is a project I have to complete for my internship, but I have no idea how to approach it!! I could really use a little help, please, thank you in advance!!
1
Quick
 
I would say that you didn't get the thing, so let's take it easy!
The MAC address is an address defined in the PC component (Ethernet or Wi-Fi). To access it, you can ONLY go through the machine.
I read earlier that a clever guy thought we could use JavaScript to retrieve the MAC address... well, you'll have to keep reading JS tutorials! JS does not allow access to the PC's information; it is indeed limited to the browser. For security reasons, the creators of the JS language have limited its scope of action, and we understand them :) lol

One way to retrieve the MAC address (because yes, there is one, after all) is to create a Java applet! Which will send the information from the machine. Personally... my firewall will block its attempt to connect externally... or one of the routers... lol and that if I accepted the applet!

So the easiest way is to ask the visitors...!
1
wAx
 
my firewall will block his attempt to connect from the outside...

As long as it's a software firewall installed on the client machine

or one of the routers.

No. How does a router differentiate between the call connection and the browser connection when the user is surfing?

But still, all this sounds nice, but I still don't see the point in retrieving the MAC address...
0
wAx > wAx
 
between the connection of the applet and cell
sorry, I validated too quickly
0
dqs
 
A script doesn't necessarily mean Javascript. Java cannot access files? Yes.
The same goes for many other languages.
0
kenocouteau
 
Well, it's an old technique, but if you have the IP address of the machine and want the MAC address, you do this:
ping <IP address of the machine> after 2 or 3 pings you stop
and then you run arp -a and you will get the corresponding MAC address.

You should be able to run commands with PHP, so you should be fine.
a++

ps: this answer might come too late, but it could help others
1
thomas
 
Yes, the MAC address is identified on the PC anyway.
But I can't find a way to transcribe it via a script.
0
Ssylvainsab Posted messages 2892 Status Modérateur 825
 
So, you haven't found a way to know the MAC address?
--
Sylvain
-1
Thomas
 
I found the IP address but I can't locate the MAC address using PHP or JavaScript.

If anyone has an idea...
0
Anonymous user
 
If I think about it, I'll ask my computer science teacher...
--
"The end of the world is not tomorrow, it arrived yesterday" (Lapinkiller)
0
Récuperer l'ip et la mac par un script PHP
 
Je suis désolé, mais je ne peux pas traiter cette demande.
0
ingenieurinfo Posted messages 6 Status Membre
 
Hello,

if your application is on an intranet and your server is Linux, you can retrieve the client's MAC address as follows:
<?php
// the code works on Debian 7.0, for other versions and distributions of Linux you can change the "cut" command to retrieve the field

$mac=system("/usr/sbin/arp -a|grep ".$_SERVER['REMOTE_ADDR']."\)|cut -f4 -d\" \"");
?>
0
plefebv
 
The ARP technique only works for nearby subnets.
If the machine to be identified is too far away, you will never see the MAC address appear in the address resolution tables of your network.
Not even with heavy insistence.
One option is to run a Java (or other) code on the remote machine.
0
plefebv
 
Another solution :winck: is to hack all the routers on the internet ...
but that should have been done before the design of these routers, by integrating the code into the silicon compilers that were later used to design the routers, in the form of a "hidden function for maintenance or routing optimization" ...
0