get mac and ip address with php

Friends today I am going to teach you to get MAC address and IP address of the Windows system with PHP. The trick is very simple don’t be afraid. You just need to grab MAC and IP of the system with PHP and then you will use it for any purpose where it required. Hope you will enjoy this tutorial. So let’s start our journey.

Problem (Get MAC and IP Address)

Suppose You have a PHP based software and now want to secure your software to run on one system only. Therefore, you need some uniqueness of the system OR sometimes or need MAC and IP address for security reasons.

Solution (Finding MAC and IP address with PHP functions)

So what will you do? I think you will find some way to find a unique system ID. Using this ID you will restrict the software so that it will run only on that system when it will that unique ID.

For the said purpose you need to get the MAC address and IP address of the Windows system and then you can use it for your desired purpose.

What is MAC Address?

According to Wikipedia:

media access control address (MAC address) is a unique identifier assigned to a network interface controller (NIC) for use as a network address in communications within a network segment

What is IP Address?

According to Wikipedia:

An Internet Protocol address (IP address) is a numerical label assigned to each device connected to a computer network that uses the Internet Protocol for communication

How to get MAC and IP address with PHP?

Here is code for getting MAC and IP address with PHP using PHP’s builtin system() function.

<?php
ob_start(); // Turn on output buffering
system('ipconfig /all'); //Execute external program to display output
$mycom=ob_get_contents(); // Capture the output into a variable
ob_clean(); // Clean the output buffer

$find_word = "Physical";
$pmac = strpos($mycom, $find_word); // Find the position of Physical text in array
$mac=substr($mycom,($pmac+36),17); // Get Physical Address
echo $mac;
?>

The above code will grab the MAC address from the windows system and will echo it for you. Now, let’s move to our next step. You need IP address of your host. It is very simple to function. To get the IP address of your system/server just write these little lines of code:-

<?php
$localIP = getHostByName(getHostName());
echo $localIP;
?>

As you can see that we have now two things. One is MAC address and 2nd once is IP address. So let’s write and function to combine that both and then use it for our desired purpose. Function is here:-

<?php
function get_mac(){
ob_start(); // Turn on output buffering
system('ipconfig /all'); //Execute external program to display output
$mycom=ob_get_contents(); // Capture the output into a variable
ob_clean(); // Clean the output buffer

$find_word = "Physical";
$pmac = strpos($mycom, $find_word); // Find the position of Physical text in array
$mac=substr($mycom,($pmac+36),17); // Get Physical Address
$localIP = getHostByName(getHostName());
return $mac.$localIP ;
}
?>

Explanation

I think there is nothing to explain here, every this is very clear. You just wrote a function that will get MAC and IP address and you then combine it to return a computer address block. So now it is time to use this function.

How to use it?

You can get the value of above function with a little line of code:

<?php
echo get_mac();
?>

That’s it. Now you use it for any purpose. That is up to you how you will use it. If you have any problem to understand than feel free to comment or contact us HERE.

Leave a Reply - I will show after approval