Show Visitors IP Address on Website

How do I show visitors IP address on my Website? The following tutorial explains how to easily display your visitors IP address on your Site or Blog through code, by using a PHP script. The file is actually called into the template as an image file.

How to Show Visitors IP Address on my Website

To get visitors IP address with PHP is fairly straightforward; So let's get started!

  1. First, create a new text document (getmyip.txt) on your desktop.
  2. Then, copy the following code into the getmyip.txt document.
    <?php
    function WhatsMyIP(){
    if(!empty($_SERVER['HTTP_CLIENT_IP'])){
    $myip = "Your IP is $_SERVER[HTTP_CLIENT_IP]";
    }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
    $myip = "Your IP is $_SERVER[HTTP_X_FORWARDED_FOR]";
    }else{
    $myip = "Your IP is $_SERVER[REMOTE_ADDR]";
    }
    $img_number = imagecreate(320,20);
    $backcolor = imagecolorallocate($img_number,255,255,255);
    $textcolor = imagecolorallocate($img_number,0,0,0);
    imagefill($img_number,0,0,$backcolor);
    Imagestring($img_number,2,2,2,$myip,$textcolor);
    header("Content-type: image/jpeg");
    imagejpeg($img_number); 
    }
    echo WhatsMyIP();
    ?>
  3. Next, rename the getmyip.txt document to getmyip.php and then upload it to the root of your website.
  4. Now, log into your WordPress administration panel and locate a template file where you would like to display the visitors IP address. For example; a sidebar text widget.
  5. Finally, copy the following code into your template, replacing the address with your website address.
    <img src="//yoursite.com/getmyip.php">

    What's my IP - Show Visitors IP Address

To modify or change text and background colors, edit backcolor and textcolor img_number RGB numeric values. Or to adjust the font size, edit the imagestring numeric values.

That's really all there is to it. Now your site visitors never need to ask; What's My IP?
Enjoy!