SQL PHP HTML ASP JavaScript articles and free scripts to download If you are facing any problem in viewing this page, please tell us
 

Gethostbyname function to get IP name of a domain


We can get the ip address of the host server by using the domain name. For example we want to know the ip address of google.com. We can use the PHP function gethostbyname(). This function takes the domain name as input and return the dot formatted IPv4 address. Note that this function will contact dns server so available network must be healthy at the time of running of the script. Let us try the function first.

$result = gethostbyname('plus2net.com');
echo $result;

This is the ip address of the host server of plus2net.com.

Gethostbyaddr()

Now from this information we can find out the domain name by using another function gethostbyaddr(). This function will take ip address as input and return the domain name. Let us add the above two functions and see what happens if out put of one function is used as input to other. Here is the code.

$result = gethostbyname('plus2net.com');
echo $result;
echo "<br>";
echo gethostbyaddr($result);

The output of the above code is

74.53.25.146
4runner.websitewelcome.com

Gethostbynamel()

This function gives us more information. It returns an array with all the details. To display this array we will use print_r function. Here is the code.

$result = gethostbynamel('google.com');
print_r($result);

The output of above code is here

Array ( [0] => 74.125.67.100 [1] => 74.125.127.100 [2] => 74.125.45.100 )


Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked

Sections
PHP
JavaScript
ASP
HTML
SQL
Photoshop
Articles SEO
PHP Tutorials
IP & Referrer
PHP Monthly Planner
PHP Introduction
Loops & structure
Array
Date & Time
Functions
Form Handling
File Handling
Math Functions
String Functions
GD Functions
Comment Posting
Content Management
PHP & Ajax
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.