-2

I am working on web application where I will need to generate demo mac address for each record. There will be plenty of records for which I will need demo mac address.

So how to generate unique mac address using PHP?

Note - I don't want exact mac address - I will need something unique like mac address but not UUID.

For Example - 00:17:88:00:00:00

mleko
  • 11,650
  • 6
  • 50
  • 71
Raj Sf
  • 1,408
  • 1
  • 17
  • 26

5 Answers5

6
echo implode(':', str_split(substr(md5(mt_rand()), 0, 12), 2));

https://eval.in/768041 or without hash

echo implode(':', str_split(str_pad(base_convert(mt_rand(0, 0xffffff), 10, 16) . base_convert(mt_rand(0, 0xffffff), 10, 16), 12, '0'), 2));

https://eval.in/768040

mleko
  • 11,650
  • 6
  • 50
  • 71
  • The third argument to `str_pad` which is `$pad_string` needs to be explicitly specified as `'0'`. Its default value is `' '` so invalid MAC like `"83:7c:71:eb:5 "` can get generated. – Suyash Aug 24 '22 at 19:20
  • I wanted to add a comment here that if you have `strict_types=1`, the first argument to `base_convert` needs to be casted to string: `(string) mt_rand(0, 0xffffff)` – Jacob Mulquin Nov 21 '22 at 02:30
2

With faker:

use Faker;

$faker = new Faker\Generator();
$faker->addProvider(new Faker\Provider\Internet($faker));
$mac = $faker->macAddress();
raveren
  • 17,799
  • 12
  • 70
  • 83
1

Raj Sf,

Your question is a little ambiguous in that you're not clear in if this unique id is unique for data that might be the same in different rows or if its unique data for any row or why you really need a unique ID formatted like a MAC address as opposed to just a number that increments. If there is a database involved anywhere in the back end, i'd use the database's unique key field and then use that to generate the unique id.

The below is for if you are not using a back end database:

For example if you have a table that might contain rows with the same data, you would need to add a column that is unique - the simplest being an autonumber of some sort (eg, 1,2,3,4,5 and so on). You could then generate a unique id from that autonumber if you so wanted to (for example like the answer mleko gave).

If the data itself is unique per row, you could generate the unique id from a sha of the concatenated row value (ie join all the values in the row together and then perform a sha1() on the string and split however you'd like).

Personally, I'd go with the autonumber approach as you can be sure you are going to get the right row back!

Justin
  • 56
  • 1
  • 1
  • 4
1

You could try different methods of password generating to generate a (pseudo)random string of 0-F chars:

How to generate random password with PHP? and then slicing it as mleko suggested.

You could also check out this project: https://github.com/BlakeGardner/php-mac-address

Community
  • 1
  • 1
Ion Bazan
  • 723
  • 6
  • 16
0

1.The assignment of mac addresses is regulated in the IEEE 802 standard.

2.Broadcast mac address FF:FF:FF:FF:FF:FF is not allowed.

3.Multicast mac address (first bit per first byte in binary) XXXXXXX1:XX:XX:XX:XX:XX is not allowed.

4.Manually generated mac address must contain ONE in the second bit of the first byte (first bit per first byte is 0 - not multicast, second bit per first byte is 1 - not auto generated, not a generated by the manufacturer) XXXXXX10:XX:XX:XX:XX:XX.

In that way, your manual generated mac address must be from 02+:00:00:00:00:00 to 02+:FF:FF:FF:FF:FF.

Manual generated, correct mac address with up to 7% faster algorithm:

echo '02:' . implode ( ':', str_split ( substr ( md5 ( mt_rand ( ) ), -10 ), 2 ) );