How to get a NIC MAC address in PHP?

xiaoxiao2021-03-06  63

This article is completely from the Internet English site, and in order to answer the netizen problem, write and reference. It's very simple, I hope everyone can look patient.

first i will share some basic thing of MAC address on internet.what happens look:? Because of the way that the internet was designed and the way that the IP protocol works, you would not be able to find a mac address of someone unless you were local to them. The post earlier about using ping and arp is correct. The mac address is put into the IP packet at the 2nd level of the OSI model, its a physical address ... once you go out through a router the mac address of the reported packet is changed, and is changed each and every time .. think of the mac as a "Return" address. You send out a packet and it goes across the country, and goes through 5 routers, the mac address on the packet will change each and every time it goes through a router, where the IP address will not. It's a type of routing code that the routers use to identify where the packet came from and where to send a response to.exampleMachine A Sends a packet to machine b, And it passes through firewall c and d. the packet does this: Machine A sends the packet to firewall C, firwall C repackages the packet with its mac address and looks for the quickest route to machine B, it determines that firewall D is the closest to it, so it repackages the packet with its own mac address ( Since this is where d Will send a response to if it cant get to machineine

and sends it on to D .. D does the exact same thing and sends the packet to B. Once Machine B receives the packet the entire process starts over again, the last mac address that is reported is for firewall D, but the IP points to Machine A, thats how it knows where to go.but there is still a function !! function returnMacAddress () {// This code is under the GNU Public Licence // Written by michael_stankiewicz {do not spam} at yahoo {no spam} dot com // Tested only on linux, please report bugs // WARNING: the commands 'which' and 'arp' should be executable // by the apache user; on most linux boxes the default configuration // should work fine / / Get the arp executable path $ location = `which arp`; // Execute the arp command and store the output in $ arpTable $ arpTable =` $ location`; // Split the output so every line is an entry of the $ arpSplitted Array $ arpsplitted = split ("/ n", $ arptable); // Get The Remote IP Address (The IP Address of the Client, The Browser $ Remoteip = $ GLOBALS ['Remote_addr']; // Ci cle the array to find the match with the remote ip addressforeach ($ arpSplitted as $ value) {// Split every arp line, this is done in case the format of the arp // command output is a bit different than expected $ valueSplitted = Split ("", $ value); Foreach ($ VALUESPLITTED AS $ SPLINE) {if (preg_match ("/ $ remoteip /", $ spline)) {$ ipfound = true;} // The ip address has been found, now Rescan all the string // to get the mac addressif ($ ipfound) {// rescan all the string, in case the mac address, in the string // Returned by ARP, COMES Before the ip address // (you know, murphy's Laws) Reset ($ VALUESPLITTED); Foreach ($ VALUESPLITTED AS $ SPLINE) {if (preg_match "

/[0-9A-F][0-9A-F][ :-]" ​​"[0-9A-F][0-9a-f][ :-]"."[0-9a-f] [0-9A-F] [: -] "." [0-9a-f] [0-9a-f] [: -] "." [0-9A-f] [0-9A-F] [0-9A-F] : -] "." "[0-9A-F] [0-9A-F] / I", $ SPLINE)) {RETURN $ ipFound = false;}} returnaf = false;}} Return False;} , be sure to understand the basics of the arp table since you can be sure that the obtained mac address is the right one only if the client is connected DIRECTLY to the php server, if there are routers or gateways that NAT the traffic, the returned Mac Address Will Be That of the Gateway, Not The Client Beside IT

转载请注明原文地址:https://www.9cbs.com/read-119329.html

New Post(0)