Tuesday, September 18, 2012

Design an online IP tool using PHP



Design an online IP tool, be able to detect visit IP address, convert hostname to IP address by input, and convert hostname to IP address by input, the tool is located:
http://www.jiansenlu.zoka.cc/IPtools/
or
http://swf.site40.net/IPtools/

First we define a css ID jiansen for input box:background color:
 <style type="text/css">
  #jiansen
{
    background-color: #ccffcc;
    font-family: /* "Lucida console", */ Verdana, Arial, Helvetica, sans-serif;
}
</style>



 1. To get user IP address using $_SERVER["REMOTE_ADDR"]
<?php
function getip() {
    if (isSet($_SERVER)) {
        if (isSet($_SERVER["HTTP_X_FORWARDED_FOR"])) {
            $realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
        } elseif (isSet($_SERVER["HTTP_CLIENT_IP"])) {
            $realip = $_SERVER["HTTP_CLIENT_IP"];
        } else {
             $realip = $_SERVER["REMOTE_ADDR"];
        }
    } else {
         if ( getenv( 'HTTP_X_FORWARDED_FOR' ) ) {
              $realip = getenv( 'HTTP_X_FORWARDED_FOR' );
         } elseif ( getenv( 'HTTP_CLIENT_IP' ) ) {
              $realip = getenv( 'HTTP_CLIENT_IP' );
         } else {
              $realip = getenv( 'REMOTE_ADDR' );
         }
    }
    return $realip;


echo "<h2>1. Your IP:</h2><p>";
echo "<font color='red'> Your computer's IP address is </font> <font color='blue'>".getip()."</font>";
?>


2. Find a website IP address using gethostbyname
 <FORM name="search" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Enter URL: <INPUT type="text" name="url" size=40 value=" " class="textbox" id='jiansen'>
<INPUT type="submit" name="submit" value="Get IP" class="button">
</form>
<?php
$url=strtolower(trim($_POST['url']));
$url=str_replace('http://','',$url);
$url=str_replace('https://','',$url);

If($url){
//$last = substr($url,-1);
//if($last=='/') $url=substr($url,0,-1);
//remove the string after /
if(strpos($url, '/')>0) $url=substr($url,0,strpos($url, '/'));
if($url==gethostbyname("$url"))
echo "Your URL is not valid";
else
echo "The IP address of ".$url." is ".gethostbyname("$url");

}
?>


3.  Find hostname from IP address:
<FORM name="form2" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Enter IP: <INPUT type="text" name="host" size=40 value=" " class="textbox" id='jiansen'>
<INPUT type="submit" name="submit1" value="Get host" class="button">
</form>
<?php
$host=trim($_POST['host']);

If($host){
if($host==gethostbyaddr("$host"))
echo "Your IP is not valid";
else   
echo "The hostname of IP address  ".$host." is ".gethostbyaddr("$host");
   
}   

?>

No comments:

Post a Comment