elxisIPHelper

by Elxis Team

Get visitor IP address

The elxisIPHelper helper returns information about the visitor IP address and also provides some conversion methods between IPv4 and IPv6.

Elxis by default uses the IPv6 protocol. If an IP address is in the IPv4 protocol it converts it to version 6. To access the IP helper class you don't need to include the helper file. Just use the elxisFramework obj method.

eFactory::getElxis()->obj('ip');

Available methods

clientIP

public function clientIP($forcev6=true, $tolong=false)

Returns the client's IP address (by default in IPv6 protocol version) or null if the IP can not be fetched.

$elxis->obj('ip')->clientIP();
//result: ::ffff:7f00:1 (v4 127.0.0.1 converted to v6)
$elxis->obj('ip')->clientIP(false);
//result: 127.0.0.1 (the IP address as returned by the web server - might be v4 or v6)

ipv6tolong

public function ipv6tolong($ip)

Provides an ip2long alternative for IPv6 addresses as currently ip2long works only for v4 IP addresses.


expandV6

public function expandV6($ip)

Expands IPv6 notation by replacing :: with :0


toV6

public function toV6($ip)

Converts a given IP v4/v6 to v6 format. Returns null if the IP is invalid.

$elxis->obj('ip')->toV6('127.0.0.1');
//result: ::ffff:7f00:1
$elxis->obj('ip')->toV6('2001:0db8:85a3:0000:0000:8a2e:0370:7334');
//result: 2001:0db8:85a3:0000:0000:8a2e:0370:7334

ipv6tov4

public function ipv6tov4($ip)

Converts a previously converted IP from v4 to v6, back to v4.

$elxis->obj('ip')->ipv6tov4('::ffff:7f00:1');
//result: 127.0.0.1

External links


It has been read 4555 times
elxisBrowserHelper
Next article
elxisBrowserHelper

You are free to copy, distribute and transmit the articles in this site for non-commercial purposes.
Creative Commons 3.0