Hi there,
Im trying to edit a script to work also with ipv6 not only ipv4 ips. The script is a simple insert into table bans restriction thing
I see some long2ip and ip2long called out functions in there. Tried to modify them into inet_pton and inet_ntop but with no luck.
The field(s) in the database where it stores them are in bigint.
For ipv4 it works great, but when i try to add ipv6 it doesnt add them and it says ip is invalid
Something like this should work:
$ip = '2001:0db7:75a3:0000:0000:8a3e:0470:7334'; // Some ipv6...
$long_ip = inet_pton($ip);
As far as I know that is all you need… Works for compressed ipv6 ip’s, too.
still says the same… is like it doesnt recognize the ipv6 format
I did some research for you. ( Might need it for myself in the future, too… )
Seems that some servers are not complied with the ipv6 option on. My Wamp test server did not have it on. My live server has it on. So, first, here is a check that shows if it is on in your server. You should run it first to ensure that you can check for ivp6…
if (defined('AF_INET6')) {
echo "PHP was compiled without --disable-ipv6 option";
} else {
echo "PHP was compiled with --disable-ipv6 option";
}
Also, researching for you, I found several comments which say sometimes there are trailing spaces that can cause the error. So, you need to trim your input ip.
Lastly, I found that a lot of code will first check if the incoming IP has a colon in it or a comma. Then, routes the code to either ivp4 if a comma and ivp6 if a colon to create a long for it. Simple way to tell the difference between ivp4 and ivp6…
( Hmmm, wonders what ivp5 is? ha! )
I just checked and it has ipv6 support enabled.
but does inet_pton and inet_ntop work in bigint fields? or i need another type of field in the database?
This is the old code part that is working under ipv4
Shouldnt just be a matter of changing ip2long and long2ip functions ?
If found you can save it, supposedly, using queries like this:
INSERT, etc, SET ip = INET6_ATON(INET_NTOA(ip));
Creates a 128-bit binary…
Have not tried it. And, to test, you would have to do that inside a query, of course, and not in PHP.
Also, I found that inet_pton() does not work with netmask’s. You need to strip them out. Here is code form PHP.net addressing that:
// Strip out the netmask, if there is one.
$cx = strpos($ipaddr, ‘/’);
if ($cx) {
$subnet = (int)(substr($ipaddr, $cx+1));
$ipaddr = substr($ipaddr, 0, $cx);
}
Can you please post some of the ipv6 addresses you are attempting to convert. Then, we can test locally and see if we can create a solution for you. Funny that this simple process is so complicated… Ha!
ive only tried 1 ipv6 to add
2404:e801:2001:41b:8a6:efdd:312e:6222
Is there a reason you don’t just store it as a string? Just curious on your needs for this…
One site said to just serialize it. I think the main reason to turn it into a number is to speed up access using a database indexed key. But, I think that would depend on how many accesses you need to do per minute…
string? like a normal varchar field?
Well, ipv6 format is just described as:
An IPv6 address is represented as eight groups of four hexadecimal digits, each group representing 16 bits (two octets, a group sometimes also called a hextet). The groups are separated by colons (:). An example of an IPv6 address is: 2001:0db8:85a3:0000:0000:8a2e:0370:7334
Therefore, it is really just a string and can be saved as such. But, a string can not be indexed as easy as a longint field. And, so it depends on what you need this save for, which would be the best way to store it.
well i dont really know anything about ipv6 and how it works
i was just looking at the ban system and was thinking about changing it to accept ipv6 also besides the ipv4
and the table fields were from the start under bigint and i assume yes for indexing
ive changed them to varbinary 16 (ive read some tutorials that this is how its supposed to be) and ipv4 under the old ip2long and long2ip functions in the file works great…still…
but if i change to inet_pton and inet_ntop …no results at all
Well, let’s talk about your needs first. You are NOT doing any fancy record keeping with this system.
You are most likely just checking IP’s when a user attempts to log into your site or even maybe when they attempt to view your pages. In the case of a login, they have gone to your site and entered their info. This only requires one small, vary fast query and does not need indexing really. In the case of them viewing your pages, if you block an IP, you would need to check the IP at the top of every page and redirect them to a 404-page instead. Again, that takes one small and very fast query. I do not think it matters how it is stored in this case.
Now with that said, if you have a million hits a day, that is another issue. At that level, you would want the field indexed. And, then, you would want to process this. I am thinking if you know your counts of visitors and they are high, then, we should solve this process. If not, just store them as a string.
Do you have a live site already? What are the visitor counts on it so far?
No, I don’t have a live site yet and i really don’t care what method is used to store the ips as long as i can insert ipv4 and ipv6 into the table.
But so far like i said, only ipv4 works
Why? I mean, you still have not told us what you are saving them for. If just to keep for record keeping save it as a string!
what do you mean why i save them for?
if someone needs to be blocked from the site, i will use it.(blocked at code level, not firewall level)
Not going to work. Many IP’s are dynamically assigned. All you will do is block every other user that ever gets that IP.
well yeah, that is also true, but what else can i do ?
and isn’t that the same thing with firewall blocking?
You can force a registration based on email addresses and passwords. Then, block them access if needed.
Yes, they can create a new email somewhere and get back in that way, but, it will cut them down for awhile.
And, you can block new members from certain things like this site does until they have been posting for awhile. This limits things like posting images and files…