Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect IPv6 in PHP?

I am currently detecting the user's IPv4 address and using IP blocks with the v4. However I want to capture the IPv6 and have block lists for this too, so the question is: Is it the same logic to get IPv6 as IPv4 or something different in PHP? I am just not sure if every device has a IPv4 and IPv6 or is it either one of these and the system will auto detect what format it is and store it?

like image 240
Danny Avatar asked Mar 21 '11 16:03

Danny


People also ask

How to get IPv6 address PHP?

“get ipv6 address php” Code Answer's The simplest way to collect the Client/Visitor IP address using PHP is the REMOTE_ADDR. Pass the 'REMOTE_ADDR' in PHP $_SERVER variable. It will return the IP address of the visitor who is currently viewing the webpage. I Hope it will help you.

How do I detect IPv6?

Check connection statusFor wired connection through a router, right-click “Ethernet”, and for wireless connection right-click “Wi-Fi”, and then click “Status”. Click “Details”. If you see an IP address for IPv6 within the window marked with a red box, you are connected to the IPv6 network.

How do you check if IPv4 or IPv6 is used?

By using I-P. show, you can check whether you are: On an IPv4 supported device here: https://v4.i-p.show. On an IPv6 supported device here: https://v6.i-p.show.


1 Answers

Apache reports the REMOTE_ADDR to PHP. If Apache listens on a v6 interface it is going to be the v6 address.

It is easy to differenciate. IPv4 addresses will always have a fullstop . character and IPv6 addresses will always contain a colon :.

When creating blocklists (or whitelists) you should be careful handling the v6 addresses. They can be shortened by using two consecutive colons ::. There is no guarantee that your OS will use the shorter or the full form, so your script must handle that. Check Wikipedia's IPv6 page for details.

like image 142
vbence Avatar answered Oct 01 '22 12:10

vbence