Skip to content

Instantly share code, notes, and snippets.

@leblanc-simon leblanc-simon/ip.php
Last active Aug 29, 2015

Embed
What would you like to do?
Simple IP visualisation
<?php
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
/**
* @see https://gist.github.com/cballou/2201933
*/
function getIp()
{
$ip_keys = array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR');
foreach ($ip_keys as $key) {
if (array_key_exists($key, $_SERVER) === true) {
foreach (explode(',', $_SERVER[$key]) as $ip) {
// trim for safety measures
$ip = trim($ip);
// attempt to validate IP
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) {
return $ip;
}
}
}
}
return isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : false;
}
if (isset($_GET['text'])) {
header('Content-Type: text/plain');
die(getIp());
}
?><!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8" />
<title>MyIP</title>
<style type="text/css">
html, body{margin:0;padding:0;text-align:center;font-family:monospace}
h1{font-size:6em;padding-top:3em;}
::-moz-selection{background:transparent}
::selection{background:transparent}
</style>
</head>
<body>
<h1 id="ip"><?php echo htmlentities(getIp()) ?></h1>
<script type="text/javascript">
function selectText(b){var d=document;var a,c;if(d.body.createTextRange){a=document.body.createTextRange();a.moveToElementText(b);a.select()}else{if(window.getSelection){c=window.getSelection();a=document.createRange();a.selectNodeContents(b);c.removeAllRanges();c.addRange(a)}}}
selectText(document.getElementById('ip'));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.