Generate Random IP address using PHP

$ip = long2ip(mt_rand());

This way is slightly more readable.


On 64-bit PHP:

long2ip(rand(0, 4294967295));

Working in 2021 in any supported PHP version (7.4 and 8.0).

Note: since almost any machine is x64 nowadays and the development of 32-bit operating systems is being abandoned, if this is not working you may probably want to download the x64 version of PHP.


Check the mt_rand func .

You'll probably want to run this :

<?php
    $randIP = mt_rand(0, 255) . "." . mt_rand(0, 255) . "." . mt_rand(0, 255) . "." . mt_rand(0, 255);
?>

Tags:

Php

Numbers