phpBB IP address encoder/decoder

phpBB stores IP addresses in its database but encodes them first.
Sometimes you need to know what's behind this encoded IP.
Here's the tool.


* Note: For the decoding functionality, you can enter multiple encoded IP addresses, one per line.

Talking about IP addresses, here's yours: 54.146.5.43 which encoded looks like 3692052b and resolves to ec2-54-146-5-43.compute-1.amazonaws.com

For your reference, here's the source code for this tool.

<html>
    <head>
        <title>phpBB IP address encoder/decoder</title>
        <style type="text/css">
            .result {
                border: solid 1px orange;
                padding: 5px;
                margin: 10px;
            }
            body, td, th {
                font-family: Verdana;
                font-size: 12px;
            } 
        </style>
    </head>
    
    <body>
        <h2>phpBB IP address encoder/decoder </h2>
        phpBB stores IP addresses in its database but encodes them first.
        <br />
        Sometimes you need to know what's behind this encoded IP.
        <br />
        Here's the tool.
        <?php

        
/**
         * This function is taken as is from phpBB
         */
        
function decode_ip($int_ip)
        {
            
$hexipbang explode('.'chunk_split($int_ip2'.'));
            return 
hexdec($hexipbang[0]). '.' hexdec($hexipbang[1]) . '.' hexdec($hexipbang[2]) . '.' hexdec($hexipbang[3]);
        }

        
/**
         * This function is taken as is from phpBB
         */
        
function encode_ip($dotquad_ip)
        {
            
$ip_sep explode('.'$dotquad_ip);
            return 
sprintf('%02x%02x%02x%02x'$ip_sep[0], $ip_sep[1], $ip_sep[2], $ip_sep[3]);
        }

        
$_GET['q'] = trim(@$_GET['q']);

        
// check if decoding was requested
        
if (!empty($_GET['decode'])) {

            echo 
'<div class="result">';

            if (empty(
$_GET['q'])) {
                echo 
'No encoded IP entered. Please give me something.';
            } else {
            
                
$input explode("\n",$_GET['q']);
                echo 
'<table border="1"><tr><th>Encoded</th><th>Decoded</th><th>Resolved</th></tr>';
            
                foreach (
$input AS $encoded) {
                    
$decoded decode_ip($encoded);
                    echo 
'<tr><td>' $encoded '</td>';
                    echo 
'<td>' $decoded '</td>';
                    echo 
'<td>' gethostbyaddr($decoded) . '</td></tr>';
                }
                
                echo 
'</table>';
            }
            echo 
'</div>';
        }


        
// check if encoding 
        
if (!empty($_GET['encode'])) {

            echo 
'<div class="result">';

            if (empty(
$_GET['q'])) {
                echo 
'No IP entered. Please give me something.';
            } else {

                
$ip_parts explode('.'$_GET['q']);
                if (
count($ip_parts) != 4) {
                    echo 
'Malformed IP address given. Give me something like 123.456.789.123, something with three dots in it.';
                } else {
                    echo 
'Supplied IP address: ' $_GET['q'];
                    echo 
'<br />Encoded IP address: ' encode_ip($_GET['q']);
                }
            }
            echo 
'</div>';
        }


        
// check if resolve 
        
if (!empty($_GET['resolve'])) {

            echo 
'<div class="result">';

            if (empty(
$_GET['q'])) {
                echo 
'No IP entered. Please give me something.';
            } else {

                
$ip_parts explode('.'$_GET['q']);
                if (
count($ip_parts) != 4) {
                    echo 
'Malformed IP address given. Give me something like 123.456.789.123, something with three dots in it.';
                } else {
                    echo 
'Supplied IP address: ' $_GET['q'];
                    echo 
'<br />Resolved IP address: ' gethostbyaddr($_GET['q']);
                }
            }
            echo 
'</div>';
        }

        
?>


        <form action="<?php echo $_SERVER['PHP_SELF']?>">

            <textarea name="q" style="width: 280px"><?php echo @$_GET['q']; ?></textarea>
            <br />
            <input type="submit" value="Decode IP(s)" name="decode" />
            <input type="submit" value="Encode IP" name="encode" />
            <input type="submit" value="Resolve IP" name="resolve" />
            <br />
        * Note: For the decoding functionality, you can enter multiple encoded IP addresses, one per line.
        </form>

        <?php
            
echo '<br />Talking about IP addresses, here\'s yours: <strong>'
                    
$_SERVER['REMOTE_ADDR'
                    . 
'</strong> which encoded looks like <strong>' 
                    
encode_ip($_SERVER['REMOTE_ADDR']) 
                    . 
'</strong> and resolves to <strong>' gethostbyaddr($_SERVER['REMOTE_ADDR']) 
                    . 
'</strong>';

        
?>
        <hr />
        <p />
        For your reference, here's the source code for this tool.
        <p />
        <?php
            highlight_file
($_SERVER['SCRIPT_FILENAME']);
        
?>
    </body>
</html>