Jesin's Blog

Welcome to the Portal of Technology

  • Facebook
  • GitHub
  • Google+
  • RSS
  • Twitter
  • Home
  • Categories
    • Domains
    • Linux
    • Networking
    • PHP
    • Virtualization
    • Web Design
    • Web Servers
    • Windows
  • WordPress Plugins
    • Custom Error Pages
    • HTTP Digest Authentication
    • Mailgun Email Validator
  • Toolbox
    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool
  • About Me
  • Contact Me
  • Sitemap
Home ›
PHP ›
How to display IP Address in PHP

How to display IP Address in PHP

February 4, 2011 PHP Jesin A 3 Comments

php category thumbnail

PHP can be used to get the IP Address of a website visitor, IP address of the server hosting the website and resolve the IP address to get the hostname a.k.a reverse DNS records. PHP has predefined $_SERVER array variable using which IP addresses of both visitors and host can be retrieved. The gethostbyaddr() function is used to find out the hostname. The indices REMOTE_ADDR and HTTP_HOST contain the visitor IP address and host IP addressTo find out the IP address of a visitor using PHP use the $_SERVER[‘REMOTE_ADDR’] variable. For example, the following code

<?php
print "Your IP address is ".$_SERVER['REMOTE_ADDR'];
?>

will display the visitor IP address to the visitor himself. If you want to record this IP address insert the value into the database.

To know your web server’s IP address using PHP use the $_SERVER[‘HTTP_HOST’] variable.

To get the hostname of the variable use the gethostbyaddr() function with IP address as the parameter. For example

<?php
print gethostbyaddr("127.0.0.1");
?>

will print localhost. If a hostname cannot be resolved the same IP address is returned.

Related posts:

connect php with mysql thumbnailConnect PHP with MySQL Default ThumbnailDisabling PHP Display Errors php category thumbnailPHP Open Port Check Script php category thumbnailCheckbox in PHP php category thumbnailPHP Sessions Tutorial

Tags: php

Comments

  1. Pratik says

    January 10, 2014 at 2:39 pm

    Thanks Nice website 🙂

    Reply
  2. Daquan E Wright says

    October 29, 2021 at 12:53 am

    Thanks……using this to aid in a script I’m writing for my job!

    Reply

Trackbacks

  1. Blocking Google Analytics and Statcounter | Jesin's Blog says:
    June 28, 2013 at 3:21 pm

    […] user. This includes your location (Country and City), web browser used, referred page, your IP address (Google Analytics doesn’t show this info to webmasters), time on site and a wealth of other […]

    Reply

Leave a Reply Cancel reply

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

Get a wealth of information delivered to your inbox. Subscribe and never miss a single article.

  • Tutorials and howtos
  • Code, scripts and commands
  • Online Tools

* No spam, unsubscribe anytime

Hire Me

  • SSL installation and hardening (A+ on Qualys SSL test)
  • Apache & Nginx configuration
  • Email deliverability improvement (10/10 on Mail Tester & MailGenius)
  • WordPress customization, optimization and migration
  • and much more…

    Tools

    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool

    Nav

    • Home
    • About Me
    • Contact Me
    • Privacy Policy
    • Sitemap
    Vultr SSD VPS

    Creative Commons License
    Jesin's Blog by Jesin A is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
    Based on a work at websistent.com.