Skip to content

Instantly share code, notes, and snippets.

@jbhannah
Created May 23, 2011 03:38
Show Gist options
  • Save jbhannah/986186 to your computer and use it in GitHub Desktop.
Save jbhannah/986186 to your computer and use it in GitHub Desktop.
Simple PHP IPv6 detection script (http://wp.me/pnbL6-6a)
<?php
header('Content-type: application/json');
if ( array_key_exists('addr', $_GET) )
$ip = $_GET['addr'];
else
$ip = $_SERVER['REMOTE_ADDR'];
$v6 = preg_match("/^[0-9a-f]{1,4}:([0-9a-f]{0,4}:){1,6}[0-9a-f]{1,4}$/", $ip);
$v4 = preg_match("/^([0-9]{1,3}\.){3}[0-9]{1,3}$/", $ip);
if ( $v6 != 0 )
$type = "IPv6";
elseif ( $v4 != 0 )
$type = "IPv4";
else
$type = "unknown";
echo json_encode(array("ip" => $ip, "type" => $type));
?>
$(function() {
$.get("http://pub.jbhannah.net/v6.php", function(d) {
if( d.type != "unknown" )
$("#remote_ip").html("Connected via " + d.type + " from " + d.ip);
}, "json");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment