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:

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

 1. To get user IP address using $_SERVER["REMOTE_ADDR"]
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">

//$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, '/'));
echo "Your URL is not valid";
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">

echo "Your IP is not valid";
echo "The hostname of IP address  ".$host." is ".gethostbyaddr("$host");


No comments:

Post a Comment