From: Leith Bade on
I want to take $_SERVER['SERVER_NAME'] and figure out whether the user
arrived by typing an IPv6-only, IPv4-only or dual IPv4/IPv6 DNS address.

It should also handle the case where the user enters a numeric address in
one of the formats the sockets inet_addr() function can handle. Such as
IPv4/IPv6 dotted decimal, octal, hex, DWORD, etc.

So far I have thought up this:

1. Use gethostbyname($_SERVER['SERVER_NAME']) to get an address
2. Check this address to see if it is IPv4/IPv6

Will this always work?

Also what is the best way in php to check if an address is IPv4 or IPv6?

Some people scan for ":" or ".", others use filter_var(), but these both
seem clunky to me.

Can anyone think of a better way?

Thanks,
Leith Bade
leith(a)leithalweapon.geek.nz
From: Per Jessen on
Leith Bade wrote:

> I want to take $_SERVER['SERVER_NAME'] and figure out whether the use=
r
> arrived by typing an IPv6-only, IPv4-only or dual IPv4/IPv6 DNS
> address.
>=20
> It should also handle the case where the user enters a numeric addres=
s
> in one of the formats the sockets inet_addr() function can handle.
> Such as IPv4/IPv6 dotted decimal, octal, hex, DWORD, etc.
>=20
> So far I have thought up this:
>=20
> 1. Use gethostbyname($_SERVER['SERVER_NAME']) to get an address
> 2. Check this address to see if it is IPv4/IPv6
>=20
> Will this always work?

gethostbyname() does not return any IPv6 addresses. You need
getaddrinfo(), but that is AFAIK not yet implemented for php.

> Also what is the best way in php to check if an address is IPv4 or
> IPv6?

preg_match() ?



--=20
Per Jessen, Z=C3=BCrich (16.2=C2=B0C)