peerencode.php
<?php
error_reporting(E_ALL);
function validateIpAddress($ip) {
if(preg_match("/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/", $ip)) {
$parts=explode(".", $ip);
foreach($parts as $ip_parts) {
if(intval($ip_parts)>255 || intval($ip_parts)<0)
return false;
}
return true;
} else {
return false;
}
}
if(!empty($_GET['port']) && ctype_digit($_GET['port']) && validateIpAddress($_GET['ip'])) {
$encoded = strtoupper(bin2hex(pack('Nn', ip2long($_GET['ip']), $_GET['port'])));
}
?>
<html>
<head>
<style>
pre {
font-size: small;
}
.form {
vertical-align: top;
font-size: small;
}
</style>
<script>
function clearFields() {
document.getElementsById('ip').innerHTML = '';
document.getElementsById('port').innerHTML = '';
}
</script>
</head>
<body>
<h3>Encode IP + port to binary format</h3>
<p>Note: The result will be represented in hexadecimal format.</p>
<form>
<div class="form">
<label for="ip">IP</label>
<input type="text" name="ip" id="ip" />
<label for="port">Port</label>
<input type="text" name="port" id="port" size="6" />
<input type="submit" value="Encode!">
<input type="button" value="Clear" onClick="clearFields()">
</div>
</form>
<div style="clear:both;"></div>
<?php if(!empty($encoded)) { ?>
<p>
<b>Encoded result:</b>
<pre><?php echo $encoded ?></pre>
</p>
<?php } ?>
</body>
</html>