简体   繁体   中英

Different browsers, different IPs?

I'm saving the user's IP addresses by saving the value of $_SERVER['REMOTE_ADDR'] in a MySQL database. Problem is that for both Firefox and Chrome $_SERVER['REMOTE_ADDR'] is ::1 (that means localhost in IPv6 ) and for IE and Opera is 127.0.0.1 ( IPv4 ).

So, my questions are

  • Are IP versions browser-dependant? (I used to think it depended on the computer)

  • Should I create two fields in the database, one for IPv4 addresses and one for IPv6 ones?

  • Should I unify all IPs to IPv6? And how can I do this in PHP (if it's even possible)?

  1. Fairly obvious - your box is IPv6-enabled, Firefox/Chrome use IPv6 whenever available, while IE and Opera don't (or it's a off-by-default setting).

  2. Store the address in a string that's long enough to hold an IPv6 address.

  3. No, 'cause in the general case you cannot.

Use the PHP function inet_pton to convert human readable IP addresses to their packed representation. You can then store each IP address in a BINARY(16) or VARBINARY(16) field in your database.

The browser will use whatever is available. This can be IPv4 or IPv6, and that can even change during the session. On top of that keep in mind that a host can have many IPv6 addresses so it might change during the session as well.

In short: don't depend on the value of REMOTE_ADDR too much :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM