Back to Top

How to get the host name of user in PHP

gethostbyaddr function in PHP

Today I am going to post one quick article about some useful network functions in PHP.

As PHP developer, I know that PHP provides $_SERVER[‘REMOTE_ADDR’] environmental variable to get the IP address of the now you can use $_SERVER[‘REMOTE_ADDR’] with the function gethostbyaddr() to get the hostname of the user.

gethostbyaddr() function requires a string which represent an IP address.It returns the corresponding hostname. If an error occurs, it returns the IP address it was given.

Let’s see one example which uses gethostbyaddr() to get the hostname of user

$host_name = gethostbyaddr($_SERVER['REMOTE_ADDR']);
echo $host_name;

If we have access to the $_SERVER[‘REMOTE_ADDR’] variable, we simply print IP address to the browser. but with gethostbyaddr() function we can get both IP address and hostname of the user.

To attempt to convert a hostname to an IP address, you can use gethostbyname().gethostbyname() function requires a hostname as its argument. It returns an IP address or if an error occurs, the hostname you provided.

Let’s see one example of gethostbyname() function

$ip = gethostbyname(''); // its display IP address of given host
echo $ip;

And, If you want the lists of IP address of particular host by passing just Hostname, you can use the gethostbyname() function. Its returns the array of IP addresses or FALSE if hostname could not be determined.

Let’s see one example of gethostbynamel() function

$hosts = gethostbynamel('');

In gethostbynamel() function, You just need to pass the name of the domain and see what function written using the print_r () function and you will get how it works.

Further Reading:

Flickr API with PHP
To access a Remote File Using PHP

I hope you have enjoyed this quick post. Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.

Comments (3)

  1. Hi there are using WordPress to your site program? Iam a new comer to blog entire world nevertheless. Iam trying to find started and hang up my very own. I also discovered Drupal is ok. Might find my personal choice…. Useful post, many thanks.

  2. Good publish. I understand one thing more difficult on very different blogs every day. It can continually be stimulating to see content through different freelance writers and implement just a little a thing from their store. I’d prefer to make use of some while using content on my own blog if you don’t mind. Natually I’ll provide you with a link within your internet website. Thanks for discussing.

  3. I really had to show this unique blog, “gethostbyaddr function in PHP | CreativeDev”
    together with my buddies on twitter. Ionly wished to spread ur tremendous
    writing! Thanks, Vivian

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to detect browser in Joomla

Posted on 6 years ago


Create Custom Page Tabs for Facebook Page

Posted on 7 years ago


Static Methods and Properties in PHP

Posted on 7 years ago


Hello world

Updated 1 week ago

Nick Carter

In depth Understanding of Laravel Core

Updated 2 months ago


ECMAScript 6 New Features – 2

Updated 1 year ago


The Reader’s Poll – June 2015

Posted on 4 years ago


The Reader’s Poll – August 2014

Posted on 4 years ago


The Reader’s Poll – June 2014

Posted on 5 years ago


The Readers’ Poll – May 2014

Posted on 5 years ago


The Readers’ Poll – August 2013

Posted on 5 years ago