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  

4 comments:

Anonymous said...

It is very clear to understand. Thanks brother..

Unknown said...

Thanks for your information.There are lot of ways available to check website ip address. I have checked my website ip from this site Whoisxy.com

Danh Ngọc said...

Mắt là cửa sổ của tâm hồn , chính vì vậy để mắt luôn luôn sáng là quan trọng nhất , nhưng nếu mắt mờ thì bạn nên dùng thuốc bổ mắt sẽ giúp mắt sáng hơn. Sẹo làm cho mọi ngưới mất tự tin về bãn thân khi ra bên ngoài , vì vậy nên dùng kem trị sẹo mederma sẽ giúp sẹo nhanh chóng biến mất . Thâm nách khiến cho các chị em mất tự tin khi diện áo 2 dây , vì vậy nên dùng kem trị thâm nách sẽ giúp các nàng có được làn da sáng đẹp dưới nách . Muốn trắng da phải làm sao và dùng thuoc trang da tot nhat mua ở đâu , kem chống nắng nào tốt là những câu hỏi mà các chị em luôn luôn đặt ra. Collagen là sản phẩm giúp làn da trắng đep và trẻ hóa , vien uong collagen chống lại sự già cỗi ngày càng tăng của làn da , có nhiều loại collagen tốt hiện nay như super collagen + c neocell , collagen shiseido enriched và sản phẩm collagen nhat ban cũng không phải ngoại lệ.

ane semprul said...

This blog is so nice to me. I will continue to come here again and again. Visit my link as well. Good luck
obat aborsi cytotec
cara menggugurkan kandungan 1 2 3 4 bulan
obat datang bulan
obat penggugur kandungan
obat aborsi
cara menggugurkan kandungan