Link to home
Start Free TrialLog in
Avatar of Sudhindra A N
Sudhindra A NFlag for India

asked on

Find IP address of a website using javascript

Hi Experts,

In one of my project requirement, I need to check the IP address of the domains. I cannot use PHP function to get IP address.

Is there way somethig to find out the ip address from javascript?

Thanks,
Sudhindra
Avatar of StingRaY
StingRaY
Flag of Thailand image

Avatar of shobinsun
May be this would help you:

<html>
<head>
  <script type="text/javascript" src="get_ip.js.php"></script>
</head>
<body onload="displayIP();">
  <div id="usersIP"></div>
</body>
</html>

get_ip.js.php:

function displayIP() {
  document.getElementById('usersIP').innerHTML = "Your IP is <?php echo $_SERVER['REMOTE_ADDR']?>";
}

OR:

http://www.hashemian.com/tools/visitor-IP.htm
Here is another one:

<script type="application/javascript">
    function getip(json){
      alert(json.ip); // alerts the ip address
    }
</script>

<script type="application/javascript" src="http://jsonip.appspot.com/?callback=getip"></script>
Avatar of Sudhindra A N

ASKER

Hi Experts, thanks for the response..
But I am not interested in the visitor IP address or the client IP address..

Say for example..
I will key in the "www.google.com" in one text box and on click of a button I need the IP address of the google.com (entered site).

Thanks.
SOLUTION
Avatar of StingRaY
StingRaY
Flag of Thailand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why cannot you use PHP to get the ip by name?
Hi,

We have to use PHP in backend:

<html>
<head>
  <script type="text/javascript" src="get_ip.js.php"></script>
</head>
<body onload="displayIP();">
  <div id="usersIP"></div>
</body>
</html>

get_ip.js.php:

function displayIP() {
  document.getElementById('usersIP').innerHTML = "Your IP is <?php echo gethostbyname('google.com');?>";
}
I use IDServe http://www.grc.com/id/idserve.htm to look up IP addresses from domain names.  Javascript does not have access to do a DNS lookup, you would have to use AJAX to call an external lookup service that would return the IP address to you.  In PHP 5, this will do it: http://us.php.net/manual/en/function.dns-get-record.php  On Windows, you have to have PHP 5.3 or newer.
You can use AJAX for your need:

<script type="application/javascript">
    function getXMLHttp()
    {
      var xmlHttp

      try
      {
        //Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
      }
      catch(e)
      {
        //Internet Explorer
        try
        {
          xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
        {
          try
          {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
          }
          catch(e)
          {
            alert("Your browser does not support AJAX!")
            return false;
          }
        }
      }
      return xmlHttp;
    }
    function MakeRequest()
    {
      var xmlHttp = getXMLHttp();
     
      xmlHttp.onreadystatechange = function()
      {
        if(xmlHttp.readyState == 4)
        {
          HandleResponse(xmlHttp.responseText);
        }
      }
      var namevalue=encodeURIComponent(document.getElementById("ip").value);
      xmlHttp.open("GET", "get_ip.js.php?name="+namevalue, true);
      xmlHttp.send(null);
    }
    function HandleResponse(response)
    {
      document.getElementById('ResponseDiv').innerHTML = response;
    }
</script>
<html>
  <body>
  <input type="text" name="ip" id="ip">
    <input type='button' onclick='MakeRequest();' value='Submit'/>
    <div id='ResponseDiv'>
      This is a div to hold the response.
    </div>
  </body>
</html>

get_ip.js.php:

<?php
echo gethostbyname($_GET['namevalue']);
?>

Many thanks for responses.

gethostbyname is disbaled by the hosting vendor. So I cannot use that function to get the IP address.
anyway many thanks again..
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have already told you about this on day 26 of Dec. Is it not an answer?
I was also aware of that but still wanted to check with other so that there may exists any tricky way to get IP address from client (web browser).
And the reason you request for deleting this post is what I have answered. Moreover, there is no mentioned in your question that you were aware of that.
My reason and your answer may be the same but it didnt gave me the solution.
Not all questions have solutions but answers. Some questions are not possible to solve, the answers tell so. Is it reasonable to delete such questions when someone tells there is no solution for that?