Reputation: 29
is there any way to covert IPV4 to IPV6 format Or vice-versa ? When user registered or login to my site I am storing IP address in database. sometimes user's IPV6 formatted address storing. User's authentication is checking on ip address column. But next time user's IPV4 formatted ip address is getting. So that in database I am not getting this IPV4 ip because stored IPV6 address previously. and authentication of user is going to fail. So I want to convert IPV4 to IPV6.
Thanks.
Upvotes: 0
Views: 5545
Reputation: 19
Accourding to IPV4to6
I created the following function for you in PHP, gues your looking for this:
function ipv4to6($ip=false) {
if (!$ip) {
return;
}
$ipAddressBlocks = explode('.', $ip);
if (count($ipAddressBlocks) == 0) {
return;
}
$ipv6 = '';
$ipv6Pieces = 0;
foreach ($ipAddressBlocks as $ipAddressBlock) {
if ($ipv6Pieces%4 == 0 && $ipv6Pieces > 0) {
$ipv6 .= '::';
}
$ipv6Piece = dechex($ipAddressBlock);
$ipv6 .= (is_numeric($ipv6Piece) && $ipv6Piece < 10 ? '0'.$ipv6Piece : $ipv6Piece);
$ipv6Pieces = strlen(str_replace('::', '', $ipv6));
}
return $ipv6.'::/48';
}
You can use this function like:
echo ipv4to6('192.0.2.4');
This will return: c000::0204::/48
Upvotes: -2
Reputation: 1003
It sounds like you are whitelisting the IP of each user during the initial registration process. If that is the case, then the issue you are experiencing is not easily resolvable. If your users have both an IPv4 and an IPv6 address, you will not be able to control which they use to connect to your server (aside from disabling IPv6 on your server). In addition, this method will not work for users with a dynamic IP address, as they will regularly get assigned a new IP and not be able to authenticate.
It is not possible to determine the users IPv4 address from their IPv6 address, as there is no guaranteed correlation between a user's IPv4 and IPv6 address*. As others have stated, IPv6 addresses do not directly convert into IPv4 addresses, as they are different formats. IPv6 addresses are much larger numbers (128-bit vs 32-bit), so converting an IPv6 address to IPv4 would not be possible even if the standard allowed for it.
*: the only exception I can think of on the correlation between IPv4 and IPv6 would be on a LAN with each device having a static private IP. If the static IPv4 and IPv6 addresses were selected so that they correlated somehow, then it would be possible to determine a v4 address from the v6 one. This is not a likely setup for most people though, and it assumes that your server is on the same local network as the end users (so that it would see the private IPs).
Given the high probability of locking out valid users (due to dynamic IPs or IPv6), this does not seem to be a reliable method of securing your server. If you have a very specific user base and dynamic IPs are not an issue, you might be able to bind your registration page to an IPv4 address and not have it listen on the IPv6 address. That way, you could at least be sure to get their IPv4 address at registration.
Edit: rewrote my answer to be more relavent to the question
Upvotes: 1
Reputation: 522015
IPv6 is not merely a different "format". IPv6 is a complete replacement of IPv4. There's no "conversion" of an IPv4 address to an IPv6 address. Your client either has an IPv4 address or they have an IPv6 address, period. Maybe they even have both, but that would only be because they run both IP stacks in parallel and can be reached via both methods; both stacks are running entirely independently of one another though, both may suddenly change their IPs independently. They're not correlated. As far as you know, each IP (whether v4 or v6) is an entirely different client.
In short: no. IPv6 is an entirely new address range. Imagine if IPv4 was extended from 255
as the maximum value for a byte to 512
and the whole new range 256.0.0.0
through 512.512.512.512
would become available; that's what IPv6 is.
Upvotes: 4
Reputation: 617
No info about storaging type(string, binary).
You can try this http://ipv6.ztsoftware.net/ipv4-to-ipv6/ but i'm not sure that is good idea. Seems like this service provide binary conversion. As said above, format of IPv4 and IPv6 - this is two different things.
Upvotes: -1