How to Handle Ipv6 Addresses in PHP

How to handle IPv6 Addresses in PHP?

How about inet_ntop()? Then instead of chopping things into integers, you just use a varbinary(16) to store it.

Detecting an IPv6 address in PHP and storing it properly in MySQL. How?


Still, say i have a 'bans' table in MySQL. How would i go about storing the IPv6 address?

You can store it in a simple column of VARCHAR(40).

Considering a sample IPv6 max is 40 byte:

2001:0DB8:0000:0000:0000:0000:1428:57ab 

That coulmn will be able to contain IPv4 too

Storing and retrieving IPv4 and IPv6 addresses

When you only had to worry about IPv4, you could use PHP's ip2long and long2ip to quickly and easily convert an IP address to a integer. These days, however, nearly everyone should expect to come across some IPv6 addresses at some point, and that's something that's only going to increase.

A common and efficient solution to have both is to use PHP's inet_pton function (which handles both IPv4 and v6), and then store the result in a VARBINARY(16) column in your database.

So, let's say we have an IPv6 address like: FE80:0000:0000:0000:0202:B3FF:FE1E:8329

When converted with inet_pton and stored in a VARBINARY(16) column it looks like this:

0xfe800000000000000202b3fffe1e8329

You can then retrieve this from your database and display it using inet_ntop.

Eg:

echo inet_ntop($ipAddressFromDB);

This is a quick and efficient way to store IP addresses in an MySQL database.

How to handle IPv6 addresses alongside IPv4 PHP

If you want to ensure that no IPv6 connections happen can add Listen 0.0.0.0:80 to your apache configuration. But most web hosts don't support IPv6 yet anyway (ya I know, we're in 2011), so it's very unlikely that people can even connect to you via IPv6. The only reason your seeing this is because bonjour (what makes .local address work) runs over IPv6.

If you want to get your code IPv6 ready, almost no changes need to be made as most IP PHP function work on both IPv4 and IPv6. The only change I remember making was increasing the varchar datatype in the MySQL table to the max length of a IPv6 address (39).

I'm not sure that IPv6 plays by the same rules of subnet that IPv4 does, but I expect it would be quite a bit harder to validate a IPv6 address is local.

EDIT:

fe80::/10 appears to be local link addresses, it might be as simple as checking the first 4 digits.

How can I get both IPv4 and IPv6 address using PHP code?

You can't.

Only the IP address the request came from is available.

There's no reliable way to identify other IP addresses (my laptop currently has 12 IP addresses) that route to the same computer.

Quick way of expanding IPv6 Addresses with PHP

The following is a two liner, where $ip is a condensed IPv6 address. Returns expanded $ip.

Example:

$ip = "fe80:01::af0";
echo expand($ip); // fe80:0001:0000:0000:0000:0000:0000:0af0

Function:

function expand($ip){
$hex = unpack("H*hex", inet_pton($ip));
$ip = substr(preg_replace("/([A-f0-9]{4})/", "$1:", $hex['hex']), 0, -1);

return $ip;
}

using php to mask part of an ipv4 or ipv6 address

You only have to remove the exclamation mark from your condition. It should be ==false



$offset = 2;
if (strpos($row["log_ip"], ":") == false) {
$needle = ".";
$replacement = ".***.***.***";
}
else {
$needle = ":";
$replacement = ":****:****:****:****:****:****:****";
}

$row["log_ip"] = substr_replace($row["log_ip"], $replacement, stripos($row["log_ip"], $needle, $offset));

echo $row["log_ip"];



Related Topics



Leave a reply



Submit