hey there. this is my first script that im submitting here.

simply put, it:
gets the ip of a website
encodes each number to binary
makes sure each binary string has 8 characters
combines each binary string
decodes the 32 bit binary string into a link

questions, comments, and flaming accepted
(although i wouldnt like the flaming too much)

<?php
/**********************************************
* by kryptn | kryptn@gmail.com | kryptn.com
* usage: $var = get_site_addr(www.example.com)
**********************************************/
// gets web address if it is set
$web = isset($_REQUEST['web']) ? gethostbynamel($_REQUEST['web']) : null;

// form block
$html = <<<html
	<div align=center>
	<form action=''>
	<input type=text name=web><br>
	<input type=submit value=go>
	</form>
	</div>
html;

// calls function for ip
if(isset($web))
	// if is set then a link to the modified site is displayed, along with the html block
	echo '<div align="center"><a href="http://'.get_site_addr($web).'">'.$web.'</a></div>'.$form;
else
	// any other way then it just displays the html block
	echo $html;


function get_site_addr($web){
// sets the default value for $num
	$num = '';

// for every character in the variable $web, it does this loop	
	for($ipa=0; isset($web[$ipa]); $ipa++){

// if the character is a period (.) then it assigns the connected value to $ip_1 
// array and resets $num to the default value
	if($web['ipa'] == '.'){
		$ip_1[] = $num;
		$num = '';
	}

	// adds on to $num with the character its testing
	else
		$num .= $web[$ipa];
}
// for every entry in $ip_1 it does this loop
foreach($ip_1 as $key => $bin){

	// encodes from decimal to binary (4 => 100)
	$ip_bin = decbin($bin);

	// makes sure that there is eight characters in the string
	// if not, it adds a zero to the beginning
	$ip_bin = substr("00000000",0,8 - strlen($ip_bin)) . $ip_bin;

	// re-assigns the encoded binary to another array.
	// i need to work on this one
	$ip_2[] = $ip_bin;
}

// puts the four values for the $ip_2 array into one string
$bin = $ip_2[0].$ip_2[1].$ip_2[2].$ip_2[3];

// makes sure that $bin is 32 characters in length
if(strlen($bin) == '32'){

	// decodes the 32 bit binary string into a decimal number
	$ip = bin_dec($bin);

	// returns the value of ip so $ip = get_site_addr($site); works
	return $ip;
}	
}
?>

    So ... what gets passed to get_site_addr()? It looks like it's supposed to be an IP address string.

    Got a whole bunch of errors first. After fixing a typo:

            if($web['ipa'] == '.') 

    should be

            if($web[$ipa] == '.'){ 

    I get

    echo get_site_addr('127.0.0.1');
    
    Notice: Undefined offset:  3 in C:\test.php on line 36
    

    and print_r($ip_2) yields

    Array
    (
        [0] => 01111111
        [1] => 00000000
        [2] => 00000000
    )

    Which explains the error message and why there was nothing returned. What happened to the ".1", I wonder?

    The whole parsing loop could be replaced by a [man]preg_match[/man] or a [man]sscanf[/man] call. Then again, from what I can make out the whole function could be rewritten as

    function get_site_addr($ip){ return ip2long($ip);}

      ehh. sorry. i didnt know about that function in the first place. i guess i was trying to reinvent the wheel

      anyway, i didnt see the typo you were referring to.

        Write a Reply...