Getting the IP address using PHP

You can display the visitor's IP address on a webpage or you can use it for other purposes such as storing it for security (if you're running an e-commerce site), redirecting the visitor, even banning the visitor from your website if you need to.

Get IP address of a visitor

Use the REMOTE_ADDR index of the $_SERVER array to get the IP address of the visitor.

Example:
Your IP address is <?php echo $_SERVER['REMOTE_ADDR']; ?>
Output:
Your IP address is 38.107.191.109

Get IP address of a website

You can get the IP address of a website by it's URL. Include the URL as a parameter of the gethostbyname() function.

Example:
IP address of google.com is <?php echo gethostbyname('www.google.com'); ?>
Output:
IP address of google.com is 72.14.204.103
WebDev how to's
  1. Custom 404 page
  2. Paragraph diff color
  3. Get IP address
  4. Printer friendly pages
  5. Transitional doc.
  6. Frameset doc.
  7. Strict doc.
  8. Javascript print page
  9. CSS space reset
  10. Generate rand image
  11. First letter stand out
  12. First line stand out
  13. Transparent content
  14. Webpage redirection
  15. Flexible containers
  16. Paragraph/image float
  17. Ban IP address
  18. Pass protect dir's
  19. Domain redirection
  20. Search engine listing
  21. PHP errors
  22. Javascript address bar
© Copyright 2009-2010 Landofcode.com
Terms of use | Privacy policy | Copyright information