PHP Visitor IP Address

Revised 2 min, 43 sec read

PHP Obtain Visitor’s IP Address

Web developers can obtain a site visitor’s information using PHP. A web server such as Apache creates a global variable called “$_SERVER”. Server and execution environment information are stored in the global variable.

PHP can access indices of the global variable. The index “REMOTE_ADDR” contains the IP address of the user viewing the current PHP page. The IP Address can be used for visitor tracking and analyzing browsing trends.

This tutorial uses PHP and an HTTP Server.

    Tools are required:

  • Text editor.
  • Folder for web server.
  • HTTP Server.
  • Browser to view output.

Optional PHP as HTTP Server

In development environmental, PHP can be used as a web server. For more information about PHP Web Server read Ojambo.com PHP Web Development Without Web Server.

GetVisitorIP.php File

<?php
/*
 * GetVisitorIP.php
 * 
 * Copyright 2013 edward <http://ojambo.com>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 * 
 */

// Get IP Address of Visitor Not Using Proxy
$IP_Address = $_SERVER['REMOTE_ADDR'];

// Get IP Address of Visitor Using Proxy
if ( isset($_SERVER['HTTP_CLIENT_IP']) ) {
	$IP_Address = $_SERVER['HTTP_CLIENT_IP'];
} elseif ( isset($_SERVER['HTTP_X_FORWARDED_FOR'])  ) {
	$IP_Address = $_SERVER['HTTP_X_FORWARDED_FOR'];	
}

// Print IP Address Found
echo $IP_Address;
?>

The index “REMOTE_ADDR” is the non-proxy visitor’s IP address. The index “HTTP_CLIENT_IP” is set by shared routers. The index “HTTP_X_FORWARDED_FOR” is set by proxy servers.

How to Use:

    Open Browser

  • Enter path (might be IP) of web development folder.
  • Check the output of PHP code.

Demonstration:

Ojambo.com PHP Visitor IP Address Tutorial

Image Missing
Ojambo.com PHP Visitor IP Address No Proxy

Image Missing
Ojambo.com PHP Visitor IP Address Router

Image Missing
Ojambo.com PHP Visitor IP Address Proxy

Conclusion:

PHP can get the IP address of website visitors. The IP address can be used for tracking and analyzing website usage. Web developers can improve websites based on the vistor’s browsing trends.

    Recommendations:

  1. Place your PHP code in a specific folder.
  2. Do not rely on one method for obtaining information.
  3. Do not use for authentication because IP addresses can be spoofed.

🚀 Recommended Resources


Disclosure: Some of the links above are referral links. I may earn a commission if you make a purchase at no extra cost to you.

About Edward

Edward is a software engineer, author, and designer dedicated to providing the actionable blueprints and real-world tools needed to navigate a shifting economic landscape.

With a provocative focus on the evolution of technology—boldly declaring that “programming is dead”—Edward’s latest work, The Recession Business Blueprint, serves as a strategic guide for modern entrepreneurship. His bibliography also includes Mastering Blender Python API and The Algorithmic Serpent.

Beyond the page, Edward produces open-source tool review videos and provides practical resources for the “build it yourself” movement.

📚 Explore His Books – Visit the Book Shop to grab your copies today.

💼 Need Support? – Learn more about Services and the ways to benefit from his expertise.

🔨 Build it Yourself – Download Free Plans for Backyard Structures, Small Living, and Woodworking.

Comments

One response to “PHP Visitor IP Address”