简体   繁体   中英

Get ip address of host name or Convert host name to ip address in php

I have a script that use api to get ip informations of an ip address. and i get a problem of getting ip address of a domain or hostname or convert it to ip, i'm looking for a code to resolve this problem, so when visitor come to website can tape or look for ip and also domain name and get ip of domain and relative information and thank you.

index.php

<?php

require_once('ipapi.class.php');

if(isset($_GET['ip']))
$rip = $_GET['ip'];
else
$rip = $_REQUEST['REMOTE_ADDR']; // the IP address to query

$query = IPAPI::query("$rip");

echo "\t IP Information: " .$rip. "<br />";
echo "\t ISP: " .$query->isp . "<br />";
echo "\t Organization: " .$query->org . "<br />";
echo "\t City: " .$query->city . "<br />";
echo "\t Region: " .$query->regionName . "<br />";
echo "\t Country: " .$query->country . "<br />"; 

?>

<form name="input" action="#" method="get">
IP: <input type="text" name="ip">
<input type="submit" value="Submit">
</form>

ipapi.class.php

 if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) 
 { 
    $rip = getenv("HTTP_CLIENT_IP"); 
 } 
 else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) 
 { 
    $rip = getenv("HTTP_X_FORWARDED_FOR"); 
 } 
 else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) 
 { 
    $rip = getenv("REMOTE_ADDR"); 
 } 
 else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] &&       strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) 
 { 
    $rip = $_SERVER['REMOTE_ADDR']; 
 } 
 else 
 { 
    $rip = "unknown"; 
 } 

PS : rip = ip i name it rip instead of ip

我将使用nslookup将主机名解析为IP。

echo shell_exec('nslookup ' . $example_host_name);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM