Generate Random IP Addresses in PHP
Complete code tutorial with examples and best practices
[ Code Example - Quick Summary ]
Language: PHP
What: Generate random IPv4 and IPv6 addresses in PHP for network testing, API mocking, and development. This guide covers public/private IP ranges, CIDR notation, and validation.
Try it: Use our interactive Ip-addresses generator or integrate this code into your PHP application.
Generate random IPv4 and IPv6 addresses in PHP for network testing, API mocking, and development. This guide covers public/private IP ranges, CIDR notation, and validation. Looking for other languages? Check our code examples in JavaScript , Python , Java , C# , C++ , Ruby and Go or use our interactive web generator.
PHP Code Example
<?php
// Generate random IPv4 address
function randomIpv4() {
return sprintf(
"%d.%d.%d.%d",
mt_rand(0, 255),
mt_rand(0, 255),
mt_rand(0, 255),
mt_rand(0, 255)
);
}
// Generate random private IPv4 (RFC 1918)
function randomPrivateIpv4() {
$ranges = [
["10", mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)],
["172", mt_rand(16, 31), mt_rand(0, 255), mt_rand(0, 255)],
["192", "168", mt_rand(0, 255), mt_rand(0, 255)],
];
$ip = $ranges[array_rand($ranges)];
return implode(".", $ip);
}
// Generate random public IPv4 (avoiding private ranges)
function randomPublicIpv4() {
do {
$ip = randomIpv4();
} while (
preg_match("/^10\./", $ip) ||
preg_match("/^172\.(1[6-9]|2[0-9]|3[01])\./", $ip) ||
preg_match("/^192\.168\./", $ip) ||
preg_match("/^127\./", $ip) ||
preg_match("/^0\./", $ip)
);
return $ip;
}
// Generate random IPv6 address
function randomIpv6() {
$parts = [];
for ($i = 0; $i < 8; $i++) {
$parts[] = sprintf("%04x", mt_rand(0, 0xFFFF));
}
return implode(":", $parts);
}
// Generate IPv4 with CIDR notation
function randomIpv4WithCidr($minCidr = 8, $maxCidr = 32) {
$ip = randomIpv4();
$cidr = mt_rand($minCidr, $maxCidr);
return "$ip/$cidr";
}
// Validate IPv4 address
function isValidIpv4($ip) {
return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false;
}
// Validate IPv6 address
function isValidIpv6($ip) {
return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false;
}
// Generate multiple IP addresses
function generateIpAddresses($count = 10, $type = "ipv4") {
$ips = [];
for ($i = 0; $i < $count; $i++) {
$ips[] = $type === "ipv6" ? randomIpv6() : randomIpv4();
}
return $ips;
}
// Usage examples
echo "Random IPv4: " . randomIpv4() . "\n";
echo "Private IPv4: " . randomPrivateIpv4() . "\n";
echo "Public IPv4: " . randomPublicIpv4() . "\n";
echo "IPv6: " . randomIpv6() . "\n";
echo "IPv4 with CIDR: " . randomIpv4WithCidr() . "\n";
$batch = generateIpAddresses(5);
echo "Batch: " . implode(", ", $batch) . "\n";
[EXPLANATION]
This implementation uses mt_rand() for generating random octets (0-255) and sprintf() for formatting IPv4 addresses. For private IPs, it follows RFC 1918 ranges: 10.0.0.0/8, 172.16.0.0/12, and 192.168.0.0/16. IPv6 addresses use 8 groups of 4 hexadecimal digits. The filter_var() function with FILTER_VALIDATE_IP validates IP formats. CIDR notation adds a subnet mask (e.g., /24) for network testing.
Expected Output
Random IPv4: 198.51.100.42 Private IPv4: 192.168.15.234 Public IPv4: 203.0.113.89 IPv6: 2001:0db8:85a3:0000:0000:8a2e:0370:7334 IPv4 with CIDR: 198.51.100.42/24 Batch: 203.0.113.15, 198.51.100.89, 192.0.2.45, 198.51.100.123, 203.0.113.67
Common Use Cases
- Generate test IP addresses for network application testing
- Mock API responses with realistic IP data
- Create sample logs for security analysis and monitoring tools
- Populate databases with network configuration test data
- Test geolocation features with diverse IP addresses
Important Notes
-
filter_var()validates format but doesn't check if IP is reachable - RFC 1918 defines private IP ranges: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16
- 127.0.0.0/8 is reserved for loopback addresses
- IPv6 can be compressed (e.g., 2001:db8::1) but this example shows full format
- For production network testing, consider using test IP ranges like 192.0.2.0/24 (TEST-NET-1)
Try Our Interactive Generator
Don't want to write code? Use our free web-based Ip-addresses generator with instant results.
TRY IP-ADDRESSES GENERATOR →Other Programming Languages
View Ip-addresses generation code examples in JavaScript
View Ip-addresses generation code examples in Python
View Ip-addresses generation code examples in Java
View Ip-addresses generation code examples in C#
View Ip-addresses generation code examples in C++
View Ip-addresses generation code examples in Ruby
View Ip-addresses generation code examples in Go