Reputation: 21
I checked several scripts so I could display my MAC address but to no avail. I'm asking for your help! Here is what I tested so far but nothing works correctly:
<?php
require_once 'MacAddress.php'; //https://github.com/BlakeGardner/php-mac-address/blob/master/src/BlakeGardner/MacAddress.php
use BlakeGardner\MacAddress;
echo "Current MAC address: ";
var_dump(MacAddress::getCurrentMacAddress('eth0'));
echo "Randomly generated MAC address: ";
var_dump(MacAddress::generateMacAddress()); // Generat all refresh a new address
?>
- Current MAC address EC:A8:6B:F1:D2:B0 on all my phones/desktop on 4G or ADSL - Randomly generated MAC address : the MAC address output varies a little bit every time the scripted is run. I suspect it's because of the time and latency information that has changed in the count. Is there a more effective way to pull just the mac address ?
<?php
class env
{
public static $IP;
public static $MAC;
public function getIP()
{
env::$IP = $_SERVER['REMOTE_ADDR'];
}
public function getMAC($ip)
{
$macCommandString = "arp $ip | awk 'BEGIN{ i=1; } { i++; if(i==3) print $3 }'";
$mac = exec($macCommandString);
env::$MAC = $mac;
}
function __construct()
{
$this->getIP();
$this->getMAC(env::$IP);
}
}
$envObj = new env();
echo "MAC : ".env::$MAC."<br>";
echo "IP : ".env::$IP;
?>
- MAC is empty - IP is my same IP on myip.com
<?php
ob_start();
system('ipconfig /all');
$mycom=ob_get_contents();
ob_clean();
$findme = "Physical";
$pmac = strpos($mycom, $findme);
$mac = substr($mycom,($pmac+36),17);
echo $mac; // Empty
$macLookup = 'MAC Address: ';
$pos = strpos($ping, $macLookup);
if ($pos !== false) {$mac = substr($ping, $pos+strlen($macLookup), 17 );
echo $mac; // Empty
}
?>
- $mac is empty
Can you help me please ?
Upvotes: 1
Views: 13138
Reputation: 31
to get your own mac address on linux server
$mac = shell_exec("ifconfig -a | grep -Po 'HWaddr \K.*$'");
var_dump($mac);
this will give mac address of server where PHP is running
Upvotes: 0
Reputation: 168655
Current MAC address EC:A8:6B:F1:D2:B0 on all my phones/desktop on 4G or ADSL
PHP code runs on the web server.
Thus the MAC address printed out by a PHP program will be the address of the network interface on the server.
You will always get the same value because it's always checking the address of the same network interface on the server.
It doesn't matter where you are running the browser, the code will always run at the same place and will always get the same code.
If want to get the MAC address of your device that you are actually using, then you will need to run the code on that device. (this will probably mean switching to a different language).
Upvotes: 2