Get Mac Address of Php Desktop Running System?

4,866 views
Skip to first unread message

arunoff...@gmail.com

unread,
May 9, 2014, 1:59:49 AM5/9/14
to phpde...@googlegroups.com
Hi
I have an application, it needs system mac address.I get php code for mac address it work in normal php server, but it doesn't work in php desktop.

By
Arun

Czarek Tomczak

unread,
May 9, 2014, 3:26:46 AM5/9/14
to
Hi Arun,

It's hard to tell what went wrong without seeing the actual code.

I've googled and found this:

It seems a bit complex, as there may be many network adapters. In my case the ipconfig /all command gives a lot of output with many mac addresses for: local area, wireless lan, another local area, virtualbox, a few tunnel adapters.

The command "arp -a 127.0.0.1" won't work. The "arp -a 192.168.0.13" where 0.13 is my wireless adapter address also seem not to work. Even if it worked it wouldn't be much useful because you first need to know the wireless network ip address and this info can't be found in phpinfo(), as the server is listening on 127.0.0.1.

You may parse the output of the "ipconfig /all" command and select the network interface that fits you.

The "getmac" command seems to be easier to parse, it's more brief:

Physical Address    Transport Name
=================== ==========================================================
00-90-F5-E8-75-02   Media disconnected
0C-8B-FD-4E-5B-DD   \Device\Tcpip_{8482B1C2-8314-4309-9547-ADF8E3C6A222}
08-00-27-00-7C-C5   \Device\Tcpip_{E12D9144-FFA9-4394-9FEC-1CF6E5D36E82}

 In my case the "0C-8B-FD-4E-5B-DD" is the wireless mac address that is desired. The "08-00-27-00-7C-C5" is the virtualbox mac address. The first record is to be ignored. You need to make some assumptions and select the right network, it may not work all cases as you see there are many addresses to choose.

The solution that would work on my computer is to parse ipconfig /all and select the first network adapter that has both "Default gateway" and "DHCP Server" set. You can write a function that makes some guesses and have some fallbacks, but still no guarantee it you will give you the desired mac address 100% of time.

Best regards,
Czarek

arunoff...@gmail.com

unread,
May 9, 2014, 3:35:41 AM5/9/14
to phpde...@googlegroups.com
Czarek

Actul this code copy from site.....
code given below
.......................

class GetMacAddr{

var $return_array = array(); // ????MAC???????
var $mac_addr;

function GetMacAddr($os_type){
switch ( strtolower($os_type) ){
case "linux":
$this->forLinux();
break;
case "solaris":
break;
case "unix":
break;
case "aix":
break;
default:
$this->forWindows();
break;
}
$temp_array = array();
foreach ( $this->return_array as $value ){

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]/i",$value,
$temp_array ) ){
$this->mac_addr = $temp_array[0];
break;
}

}
unset($temp_array);
return $this->mac_addr;
}

function forWindows(){
@exec("ipconfig /all", $this->return_array);
if ( $this->return_array )
return $this->return_array;
else{
$ipconfig = $_SERVER["WINDIR"]."\system32\ipconfig.exe";
if ( is_file($ipconfig) )
@exec($ipconfig." /all", $this->return_array);
else
@exec($_SERVER["WINDIR"]."\system\ipconfig.exe /all", $this->return_array);
return $this->return_array;
}
}

function forLinux(){
@exec("ifconfig -a", $this->return_array);
return $this->return_array;
}

}
//????
$mac = new GetMacAddr(PHP_OS);
$mac=$mac->mac_addr;


please try it...


I want to get ethernet macaddress it's wifi address

Czarek Tomczak

unread,
May 9, 2014, 3:40:22 AM5/9/14
to phpde...@googlegroups.com
The Mongoose web server does not expose the WINDIR environment variable. The solution would be to replace $_SERVER["WINDIR"] with $_SERVER["SYSTEMROOT"]. The SYSTEMROOT variable is available in Mongoose and points to C:\Windows.

arunoff...@gmail.com

unread,
May 9, 2014, 3:42:49 AM5/9/14
to phpde...@googlegroups.com
It's Windows XP system so can't access ipconfig.exe so it's may be a problem to this code...

Now try this type code but need a help, How to get mac address from wscript.shell
to a string
No idea about how this code work.

$shell = new COM("WScript.Shell") or die("Requires Windows Scripting Host");
$some = $shell->run(getmac);

Czarek Tomczak

unread,
May 9, 2014, 3:46:21 AM5/9/14
to phpde...@googlegroups.com
The code you posted won't work in my case, won't return wifi mac address, because it is not the first network adapter on the list returned by ipconfig /all:

C:\Users\ctomczak>ipconfig /all
 
Windows IP Configuration
   Host Name . . . . . . . . . . . . : ctomczak-PC
   Primary Dns Suffix  . . . . . . . :
   Node Type . . . . . . . . . . . . : Hybrid
   IP Routing Enabled. . . . . . . . : No
   WINS Proxy Enabled. . . . . . . . : No
 
Ethernet adapter Local Area Connection* 19:
   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Juniper Network Connect Virtual Adapter
   Physical Address. . . . . . . . . : 00-FF-50-E4-1E-0D
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes
 
Wireless LAN adapter Wireless Network Connection:
   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Intel(R) Wireless-N 7260
   Physical Address. . . . . . . . . : 0C-8B-FD-4E-5B-DD
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : fe80::f455:a760:b0e5:4c13%13(Preferred)
   IPv4 Address. . . . . . . . . . . : 192.168.0.13(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Lease Obtained. . . . . . . . . . : Friday, May 09, 2014 7:45:13 AM
   Lease Expires . . . . . . . . . . : Friday, May 16, 2014 7:45:17 AM
   Default Gateway . . . . . . . . . : 192.168.0.1
   DHCP Server . . . . . . . . . . . : 192.168.0.1
   DHCPv6 IAID . . . . . . . . . . . : 386698237
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-1A-0A-4D-46-00-90-F5-E8-75-02
   DNS Servers . . . . . . . . . . . : 62.179.1.61
                                       62.179.1.63
   NetBIOS over Tcpip. . . . . . . . : Enabled

 

arunoff...@gmail.com

unread,
May 9, 2014, 4:11:28 AM5/9/14
to phpde...@googlegroups.com
I want lock my application with unique identification (Like Mac address it's not stable bcz Wifi ethernet like that have different mac address) So better option to protect my application inside....?
Message has been deleted

Czarek Tomczak

unread,
May 9, 2014, 7:48:56 AM5/9/14
to phpde...@googlegroups.com
Take a look at the KnowledgeBase wiki page "How to get a unique computer identifier?":

arunoff...@gmail.com

unread,
May 9, 2014, 7:54:37 AM5/9/14
to phpde...@googlegroups.com
Thanks you
really hats off man You did good job
Arun

Fares ABADOU

unread,
May 9, 2014, 6:59:49 PM5/9/14
to phpde...@googlegroups.com
hi, i had the same problem cant get hardware mac adress  . . and now im using PC-USERNAME  
with php function get_current_user(); . 

arunoff...@gmail.com

unread,
May 12, 2014, 1:19:51 AM5/12/14
to phpde...@googlegroups.com
hi Fares ABADOU

That's not stable to work with an application so find another idea share with us....Now am use to lock my application via system mac address but it also some drawbacks some time get mac or some time fetch mac of WiFi or ether-net like that...so not completely dependable. Czarek Tomczak say to use UUID i check this idea but error occurred one of my system how it will solved:


C:\Documents and Settings\user>wmic path win32_computersystemproduct get uuid
'wmic' is not recognized as an internal or external command,
operable program or batch file.

Czarek Tomczak

unread,
May 12, 2014, 3:00:33 AM5/12/14
to phpde...@googlegroups.com
@Arun, On some systems the wbem/ directory ma not be in PATH if user modified the environment variables, so to be sure you have to provide the full path to the wmic executable like this:

C:\Windows\System32\wbem\wmic.exe path win32_computersystemproduct get uuid

Updated the examples on the KnowledgeBase wiki page. Also added some additional notes. 

arunoff...@gmail.com

unread,
May 12, 2014, 3:12:49 AM5/12/14
to phpde...@googlegroups.com
Hi Czarek Tomczak
Great...Thank you...

chad.soc...@gmail.com

unread,
Sep 28, 2014, 12:39:16 AM9/28/14
to phpde...@googlegroups.com
Ok i am confused even after reading the wiki what is the exact code to getting the UUID?

I tried this and no good:

$shell = new COM("WScript.Shell") or die("Requires Windows Scripting Host");

$getUUID= $shell->Run("C:\Windows\System32\wbem\wmic.exe path win32_computersystemproduct get uuid");

echo "this is PC's UUID: $getUUID";

and all i get is:
this is PC's UUID: 0

it does popup for a split second a command line with what looks like the correct info but no idea how to capture the data any help?

Czarek Tomczak

unread,
Sep 28, 2014, 1:30:08 AM9/28/14
to phpde...@googlegroups.com
@chad It's explained on KnowledgeBase wiki page:
And later on that wiki page a solution to such case:

You have to decide which ones will decide about uniqueness of a computer. You may want to query several of them and not depend only on one, as sometimes it may not be available, or be of zeroes like uuid. 

chad.soc...@gmail.com

unread,
Sep 28, 2014, 1:33:28 AM9/28/14
to phpde...@googlegroups.com
OH ok, so what would be best method? i just want anything to identify besides the IP address so we can keep track no one is using our app without permission

Czarek Tomczak

unread,
Sep 28, 2014, 1:56:13 AM9/28/14
to phpde...@googlegroups.com
@chad It's up for you to to decide whether to use Mac address, MachineGuid or a mix of software/hardware information.

The code you provided earlier was incorrect. 0 was a status of a command, not an output. Use something like this:
ob_start();
$ret = system("C:\Windows\System32\wbem\wmic.exe path win32_computersystemproduct get uuid");
$output = ob_get_contents();
ob_end_clean();
if ($ret !== false) {
    echo "Command executed successfully.<br>";
    echo "Output: $output";
} else {
    echo "Command failed.";
}

chad.soc...@gmail.com

unread,
Sep 28, 2014, 9:58:29 AM9/28/14
to phpde...@googlegroups.com
that never responds its just sits there and nothing echos to the screen

Czarek Tomczak

unread,
Oct 3, 2014, 1:05:47 PM10/3/14
to phpde...@googlegroups.com

chad.soc...@gmail.com

unread,
Oct 3, 2014, 8:33:39 PM10/3/14
to phpde...@googlegroups.com
i just made a .bat file to garb info saved as a txt file and then opened that in php and got the UUID that the bat file made... here is what i did if it helps anyone or if anyone knows a faster or better way

Bat file:
@echo off
cls
setlocal EnableDelayedExpansion

:: now lets get UUIDD
wmic csproduct get UUID >output.tmp

Type output.tmp | findstr /I /V /C:"UUID">>UUID.tmp
for /f "tokens=1*delims=:" %%G in ('findstr /n "^" UUID.tmp') do if %%G equ 1 SET UUID=%%H
echo UUID=%UUID% >user.txt
echo USER=%USERNAME% >>user.txt

:: now delete tmp file(s)
Del output.tmp
Del UUID.tmp

EXIT

------

I got the username just in case as well... and then in my php i did this:
<?php

// now lets get uuid info using bat file
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run('cmd /C user/getUserInfo.bat', 2, false);
unset($WshShell, $oExec);

// lets get user settings
$myFile = "user/user.txt";
$lines = file($myFile);//file in to an array
$get= $lines[0];
$split = explode("=", $get);
$UUIDD = $split['1'];


?>

----------------------

i am sure there is a better way but for now it works, any suggestions is great

ARUNKUMAR VK

unread,
Oct 4, 2014, 12:33:08 AM10/4/14
to phpde...@googlegroups.com
 
For every one this code help you ....................


$shell = new COM("WScript.Shell") or die("Requires Windows Scripting Host");
$command="echo | wmic.exe path win32_computersystemproduct get uuid";
$a=shell_exec($command);

But "Czarek" says correct. my experiment i found that
1. UUID same with different system (Tested on two XP os machine)

2.One system UUID return null value like
(FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF) space are stripped
    11111111111111111111111111111111
these are i find issue use of UUID as our application protection....
Take 100 system 2 machine it's flop, so  take your own decision to use UUID or Not

Thank you Supporting my question.....

Czarek Tomczak

unread,
Oct 4, 2014, 2:25:28 AM10/4/14
to phpde...@googlegroups.com
Thanks Arunkumar for the fix to the hang issue, works great. I've updated Issue 124 and the knowledgebase wiki page with your code example. You should use a full path to wmic.exe as in some cases wmic.exe may not be in env PATH. 

Added a note on the knowledgebase wiki page about UUID not necessarily being unique.  It's best to query multiple software/hardware information and create unique hash based on these values.

ARUNKUMAR VK

unread,
Oct 4, 2014, 2:53:51 AM10/4/14
to phpde...@googlegroups.com
Czarek Tomczak Thank you for your encouragements...

And guys if any one got better idea for protect our application.please share your idea this group (Just logic. At least thread)

Kobit Bilişim

unread,
Oct 14, 2014, 3:28:36 AM10/14/14
to phpde...@googlegroups.com
TRY THIS CODE:
                $info = array();
               $uuid = shell_exec("echo | {$_ENV['SYSTEMROOT']}\System32\wbem\wmic.exe path win32_computersystemproduct get uuid");
           $lines = explode("\n", $uuid);
         $info['UUID'] = trim($lines[1]);
               $output = shell_exec("echo | ipconfig/all");
           if ($output) {          
                       $lines = explode("\n", $output);
                       $group = '';
                   foreach($lines as $line){
                              if(strpos($line, " . ") !== FALSE) {
                                   $key = str_replace('.','',trim(substr($line,0,strpos($line, " . "))));
                                 $value = trim(substr($line,strpos($line, " : ") + 3));
                                 $info[$group]["{$key}"] = "{$value}";
                          } else if(!empty($line)){
                                      if(substr($line,0,1) == ' '){
                                  }else{
                                         $group = $line;
                                        $info["$group"] = array();
                                     }
                              } else {

                                }                      
                        }
              }
                echo '<pre>';
               print_r($info);
               echo '</pre>';




ARUNKUMAR VK

unread,
Oct 14, 2014, 5:43:27 AM10/14/14
to phpde...@googlegroups.com
Hi Kobit Bilişim

Your code is correct. But we need better logic to protect our phpdesktop application. When we done a project in phpdesktop it's commercial use we know each product have uniqueness (Identify the each customers my logic is UUID or take hard disk serial number  and some extra values make for a Product Key ). Is there any good idea?? please share with us

naruk...@inovies.com

unread,
Sep 5, 2015, 7:53:03 AM9/5/15
to PHP Desktop
I am not getting with all above codes in server????

manish....@gmail.com

unread,
Dec 21, 2016, 3:39:04 AM12/21/16
to PHP Desktop
online code error

manish....@gmail.com

unread,
Dec 21, 2016, 3:39:31 AM12/21/16
to PHP Desktop
Reply all
Reply to author
Forward
0 new messages