Duminda Chamara

Saturday, November 3, 2012

How to get ip address of a website

How to determine the IP Address of a website in php?


Sometimes you may need to get the IP address of a website using php, in such cases you can do it as follows. There are in built methods to get the IP address against a domain name in php and you can easily to it. You can get all IPv4 address and IPv6 addresses for a given internet host name. You may need to connect to internet before testing this examples.

1. Example code to get the Host IP Address using php.

<?php
/*
©-Copyright by kottawadumi.blogspot.com
*/

$host_name = "facebook.com";
$ip_address = gethostbyname($host_name);
echo "The IP Address of ".$host_name." is ".$ip_address.'<br/>';

$website_name = "wikipedia.org";
$ipAddress = gethostbyname($website_name);
echo "The IP Address of ".$website_name." is ".$ipAddress.'<br/>';

?>

This example is based on gethostbyname php in built method. It may usually take 0.5 ~ 0.2 seconds to DNS Resolution. Make sure that you have connected to the internet testing this code. If not gethostbyname function returns the host name instead of the IP address.

2. How to get a list of IPv4 addresses of a given host name.

<?php
/*
©-Copyright by kottawadumi.blogspot.com
*/


$host_name = "google.com";
$ip_addresses = gethostbynamel($host_name);

if($ip_addresses){
echo "IP Addresses of ".$host_name.'<br/>';
foreach($ip_addresses as $address){
echo $address.'<br/>';
}
}else{
echo 'Host Not Found';
}

?>

In this example, it uses gethostbynamel php in build method to get IP Address details against a host name. It returns an array of IP addresses in successful lookup and returns false, when lookup fails to determine the host.

3. Example code for find IPv4 Address of a given host in php.

<?php
/*
©-Copyright by kottawadumi.blogspot.com
*/


$host_name = "facebook.com";
$result = dns_get_record($host_name,DNS_A);

echo "IP v4 Addresses of ".$host_name.'<br/>';

foreach($result as $ip_info){
echo $ip_info['ip'].'<br/>';
}

?>

In this example, It uses a dns_get_record method to find 32-bit IPv4 Address (Internet Protocol version 6). It Fetches DNS Resource Records associated with the given host name. Usually there can be more than one IP address. This will returns Authoritative Name Servers. Authoritative Name Servers means domain names that have been specifically configured by the server administrator.

dns_get_record function lookups DNS Resource Records associated with the given host name. Here you need only IP address. So you can limit the DNS query using DNS_A as second parameter.

4. How to get IPv6 Address of a given host using php.

<?php
/*
©-Copyright by kottawadumi.blogspot.com
*/


$host_name = "facebook.com";
$result = dns_get_record($host_name,DNS_AAAA);

echo "IP v6 Addresses of ".$host_name.'<br/>';

foreach($result as $ip_info){
echo $ip_info['ipv6'].'<br/>';
}

?>

In this example, It uses a dns_get_record method to find 128-bit IPv6 address (Internet Protocol version 6). You can pass the DNS_AAAA as the second parameter.It returns IP version 6 addresses.





©-Copyright By Duminda Chamara  Java Script Validation