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 visitor.so 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

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

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

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.

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

Most Popular Posts

MySQL REGEXP Operator

Posted on 6 years ago

Bhumi

How to Set the Time Zone in Modx

Posted on 11 years ago

Bhumi

Mysql String Function FIND_IN_SET( )

Posted on 12 years ago

Bhumi