The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Getting the IP address of the server

Discussion in 'cPanel Developers' started by k_shehadeh, Jul 20, 2009.

  1. k_shehadeh

    k_shehadeh Member

    Joined:
    Mar 27, 2009
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    1
    What's the preferred way of getting the IP address of a server via the API and only when connecting via port 2082/83 (not WHM)?
     
  2. chirpy

    chirpy Well-Known Member

    Joined:
    Jun 15, 2002
    Messages:
    13,475
    Likes Received:
    20
    Trophy Points:
    38
    Location:
    Go on, have a guess
    Which IP address are you after? The servers main IP address or the IP address of the site you're running the script under? Could you use the apache SERVER_ADDR environment variable?
     
  3. k_shehadeh

    k_shehadeh Member

    Joined:
    Mar 27, 2009
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    1
    Actually, I'm accessing the API remotely via Javascript. The IP address that I'm trying to get is the IP address of the server on which Cpanel is running.
     
  4. MattDees

    MattDees cPanel Product Owner
    Staff Member

    Joined:
    Apr 29, 2005
    Messages:
    417
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
  5. k_shehadeh

    k_shehadeh Member

    Joined:
    Mar 27, 2009
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    1
    Thanks, Matt! That worked. I was using "dedicatedip" and on some servers it wasn't returning an ip address.
     
  6. krish_ven

    krish_ven Registered

    Joined:
    Aug 24, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    function countryCityFromIP($ipAddr)
    {
    //function to find country and city from IP address
    //Developed by Roshan Bhattarai Roshan Bhattarai's Blog - A blog about PHP, Ajax, JavaScript, CSS, Web 2.0 and Web Development

    //verify the IP address for the
    ip2long($ipAddr)== -1 || ip2long($ipAddr) === false ? trigger_error("Invalid IP", E_USER_ERROR) : "";
    $ipDetail=array(); //initialize a blank array

    //get the XML result from hostip.info
    $xml = file_get_contents("http://api.hostip.info/?ip=".$ipAddr);

    //get the city name inside the node <gml:name> and </gml:name>
    preg_match("@<Hostip>(\s)*<gml:name>(.*?)</gml:name>@si",$xml,$match);

    //assing the city name to the array
    $ipDetail['city']=$match[2];

    //get the country name inside the node <countryName> and </countryName>
    preg_match("@<countryName>(.*?)</countryName>@si",$xml,$matches);

    //assign the country name to the $ipDetail array
    $ipDetail['country']=$matches[1];

    //get the country name inside the node <countryName> and </countryName>
    preg_match("@<countryAbbrev>(.*?)</countryAbbrev>@si",$xml,$cc_match);
    $ipDetail['country_code']=$cc_match[1]; //assing the country code to array

    //return the array containing city, country and country code
    return $ipDetail;

    }
    you can chk with this php code & you can get the DNS server ip address by giving domain name & get the ip address from ip-details.com
     
Loading...

Share This Page