I'm currently doing a website that shows the visitor's ip that includes IPv4 and IPv6 but it seems that I can't display them together if the visitor has both ips.
<p>IPv4: <?php
if(filter_var($getIP, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
echo $remoteIP;
}
else {
echo "N/A";
}
?></p>
<p>IPv6: <?php
if ( ipv6::isIPv6($remoteIP) ) {
echo $remoteIP;
} else {
echo "N/A";
}
?></p>
GetIPv6 This code checks for user if he has IPv6 as his IP
<?php
class ipv6 {
function get_ip() {
return getenv("REMOTE_ADDR");
}
function isIPv6($ip = "") {
if ($ip == "") {
$ip = ipv6::get_ip();
}
if (substr_count($ip,":") > 0 && substr_count($ip,".") == 0) {
return true;
} else {
return false;
}
}
}
$remoteIP= $_SERVER['REMOTE_ADDR'];
GetIPv4
Just getting the IPv4, the most standard ip
<?PHP
function getUserIP()
{
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
if(filter_var($client, FILTER_VALIDATE_IP))
{
$ip = $client;
}
elseif(filter_var($forward, FILTER_VALIDATE_IP))
{
$ip = $forward;
}
else
{
$ip = $remote;
}
return $ip;
}
$getIP = getUserIP();
?>