How to Get Device MAC Address in PHP
How to Get Device MAC Address in PHP

How to Get Device MAC Address in PHP

How to Get Device MAC Address in PHP

Hi guys, in this article, we will learn How to Get Device MAC Address in PHP. So let’s get started.

In PHP, we can use the exec function to execute a command that retrieves the MAC address of a network interface. However, this approach may not work on all systems and may require appropriate permissions. Below is an example of how you can retrieve the MAC address using the ifconfig command (Linux/macOS) or ipconfig command (Windows):

Get MAC address in PHP For Linux/macOS:

function getMacAddress() {
    $output = shell_exec('ifconfig eth0'); // replace 'eth0' with the desired network interface

     if (preg_match('/(\w\w:\w\w:\w\w:\w\w:\w\w:\w\w)/', $output, $matches)) 
     {
        return $matches[0];
     } 
     else 
     {
        return "MAC address not found";
     } 
}

$macAddress = getMacAddress();

echo "MAC Address: " . $macAddress;

 

Get MAC address in PHP in Windows Operating System:

function getMacAddress() {
    $output = shell_exec('ipconfig /all');

     if (preg_match('/Physical\s+Address[^:]*:\s*([0-9a-fA-F\-:]+)/', $output, $matches)) 
     {
        return str_replace('-', ':', $matches[1]);
     } 
     else 
     {
        return "MAC address not found";
      }
}

$macAddress = getMacAddress();
echo "MAC Address: " . $macAddress;

Check Out More Resources:

Articles:
Website: https://laravelplug.com/
YouTube Channel: https://www.youtube.com/channel/UCnTxncHQcBSNs4RCuhWgF-A?sub_confirmation=1
WordPress Playlist: https://www.youtube.com/watch?v=8VvXzFAFwQU&list=PLVoVZqAh8dTLHZ51egvEU7dsOrRCSIMWx
Tools Playlist: https://www.youtube.com/watch?v=gTQoUn3BQkM&list=PLVoVZqAh8dTK-DWHBGWOEEvzvPQrgFne-

WordPress Tutorials: https://laravelplug.com/category/wordpress/
Laravel Tutorials: https://laravelplug.com/category/laravel/
PHP Tutorials: https://laravelplug.com/category/php/
SQL Tutorials: https://laravelplug.com/category/sql/
Various Tips: https://laravelplug.com/category/tips/
Useful Tools: https://laravelplug.com/category/tools/

Socials:

Twitter: https://twitter.com/LaravelPlug
Pinterest: https://www.pinterest.com/LaravelPlugCom/
Facebook: https://www.facebook.com/groups/1020759861842360
Mail: info@laravelplug.com

#laravel #guide #php

Editorial Staff

A Learner and trying to share what I learned!