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
1 2 3 4 | $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
1 2 3 4 | $ip = gethostbyname('www.creativedev.in'); // 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
1 2 3 4 | $hosts = gethostbynamel('www.thecreativedev.com'); print_r($hosts); |
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)