Created
May 23, 2011 03:38
-
-
Save jbhannah/986186 to your computer and use it in GitHub Desktop.
Simple PHP IPv6 detection script (http://wp.me/pnbL6-6a)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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)); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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